[英]why do I got Uncaught TypeError: response.text is not a function" error when try to assignt body received as text to a variable in javascript
I got a mock server who responds with a body text like this: "2020-05-02 2020-05-05 2020-05-07 2020-05-15 2020-05-16" then I try to insert each o these elements in an javascript array using this func:我有一个模拟服务器,它用这样的正文文本进行响应:“2020-05-02 2020-05-05 2020-05-07 2020-05-15 2020-05-16” 然后我尝试插入每个元素在一个 javascript 数组中使用这个函数:
<script>
$(document).ready(function() {
let response = fetch("http://127.0.0.1:35980/returnBalancesDates")
let value = response.text();
var choices = value.split(' ');
console.log(choices)
var x = document.createElement("SELECT");
x.setAttribute("id", "mySelect");
x.setAttribute("name", "oldbaldate");
document.body.appendChild(x);
for (i = 0; i < choices.length; i = i + 1) {
var z = document.createElement("option");
z.setAttribute("value", choices[i]);
var t = document.createTextNode(choices[i]);
z.appendChild(t);
document.getElementById("mySelect").appendChild(z);
}
$(x).appendTo('#selectOldBalance');
});
</script>
In browser console I see this error and my select element is not created in html page:在浏览器控制台中,我看到此错误,并且我的 select 元素未在 html 页面中创建:
Uncaught TypeError: response.text is not a function
at HTMLDocument.<anonymous> ((index):75)
at e (jquery.min.js:2)
at t (jquery.min.js:2)
Fetch is async, so when you call response.text()
you actually dont have the response yet. Fetch 是异步的,因此当您调用
response.text()
时,您实际上还没有响应。 Either use response = await fetch
or callbacks.使用
response = await fetch
或回调。
I got a mock server who responds with a body text like this: "2020-05-02 2020-05-05 2020-05-07 2020-05-15 2020-05-16" then I try to insert each o these elements in an javascript array using this func:我得到了一个模拟服务器,它以如下正文响应:“2020-05-02 2020-05-05 2020-05-07 2020-05-15 2020-05-16”然后我尝试插入这些元素中的每一个在 javascript 数组中使用这个函数:
<script>
$(document).ready(function() {
let response = fetch("http://127.0.0.1:35980/returnBalancesDates")
let value = response.text();
var choices = value.split(' ');
console.log(choices)
var x = document.createElement("SELECT");
x.setAttribute("id", "mySelect");
x.setAttribute("name", "oldbaldate");
document.body.appendChild(x);
for (i = 0; i < choices.length; i = i + 1) {
var z = document.createElement("option");
z.setAttribute("value", choices[i]);
var t = document.createTextNode(choices[i]);
z.appendChild(t);
document.getElementById("mySelect").appendChild(z);
}
$(x).appendTo('#selectOldBalance');
});
</script>
In browser console I see this error and my select element is not created in html page:在浏览器控制台中,我看到此错误,并且我的 select 元素未在 html 页面中创建:
Uncaught TypeError: response.text is not a function
at HTMLDocument.<anonymous> ((index):75)
at e (jquery.min.js:2)
at t (jquery.min.js:2)
This is the script that worked for me:这是对我有用的脚本:
<script>
$(document).ready(function() {
fetch('http://127.0.0.1:35980/returnBalancesDates')
.then(dataWrappedByPromise => dataWrappedByPromise.text())
.then(data => {
console.log(data)
var choices = data.split(' ');
console.log(choices);
var x = document.createElement("SELECT");
x.setAttribute("id", "mySelect");
x.setAttribute("name", "oldbaldate");
document.body.appendChild(x);
for (i = 0; i < choices.length; i = i + 1) {
var z = document.createElement("option");
z.setAttribute("value", choices[i]);
var t = document.createTextNode(choices[i]);
z.appendChild(t);
document.getElementById("mySelect").appendChild(z);
}
$(x).appendTo('#selectOldBalance');
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.