The text()
method of the Body
mixin takes a Response
stream and reads it to completion. It returns a promise that resolves with a USVString
object (text). The response is always decoded using UTF-8.
Syntax
response.text().then(function (text) { // do something with the text response });
Parameters
None.
Return value
A Promise that resolves with a USVString
.
Example
In our fetch text example (run [[../../../../../../../mdn.github.io/fetch-examples/fetch-text/index|fetch text live]]), we have an <article>
element and three links (stored in the myLinks
array.) First, we loop through all of these and give each one an onclick
event handler so that the getData()
function is run — with the link's data-page
identifier passed to it as an argument — when one of the links is clicked.
When getData()
is run, we create a new request using the Request()
constructor, then use it to fetch a specific .txt
file. When the fetch is successful, we read a USVString
(text) object out of the response using text()
, then set the innerHTML
of the <article>
element equal to the text object.
let myArticle = document.querySelector('article');
let myLinks = document.querySelectorAll('ul a');
for(let i = 0; i <= myLinks.length-1; i++) {
myLinks[i].onclick = function(e) {
e.preventDefault();
let linkData = e.target.getAttribute('data-page');
getData(linkData);
}
};
function getData(pageId) {
console.log(pageId);
var myRequest = new Request(pageId + '.txt');
fetch(myRequest).then(function(response) {
return response.text().then(function(text) {
myArticle.innerHTML = text;
});
});
}
Specifications
Specification | Status | Comment |
---|---|---|
FetchThe definition of 'text()' in that specification. | Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Chrome Full support 42 Full support 42 Full support 41 Disabled' From version 41: this feature is behind the |
Edge
Full support ≤18 |
Firefox Full support 39 Full support 39 Full support 34 Disabled' From version 34: this feature is behind the |
IE
No support No |
Opera Full support 29 Full support 29 Full support 28 Disabled' From version 28: this feature is behind the |
Safari
Full support 10.1 |
WebView Android
No support No |
Chrome Android
Full support 42 |
Firefox Android
No support No |
Opera Android Full support 29 Full support 29 Full support 28 Disabled' From version 28: this feature is behind the |
Safari iOS
Full support 10.3 |
Samsung Internet Android
Full support 4.0 |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.'
- Experimental. Expect behavior to change in the future.
- User must explicitly enable this feature.'
- User must explicitly enable this feature.
See also
Body.text() by Mozilla Contributors is licensed under CC-BY-SA 2.5.