text()
方法在
Body
mixin takes a
响应
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.
response.text().then(function (text) {
// do something with the text response
});
None.
A Promise that resolves with a
USVString
.
In our
fetch text example
(run
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.
当
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
的
<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;
});
});
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
Fetch
The definition of 'text()' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
text
|
Chrome
42
|
Edge ≤18 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10 | WebView Android No | Chrome Android 42 | Firefox Android No |
Opera Android
29
|
Safari iOS 10 | Samsung Internet Android No |
完整支持
不支持
实验。期望将来行为有所改变。
用户必须明确启用此特征。
Body
arrayBuffer()
blob()
formData()
json()
text()