简体   繁体   English

为什么在尝试将作为文本接收的正文分配给 javascript 中的变量时出现未捕获类型错误:response.text 不是函数”错误

[英]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.

相关问题 fetch body (response.text()) 返回空 - fetch body (response.text()) is returning empty 为什么收到错误消息:Uncaught TypeError:无法处理绑定“ text:function(){return $ data.tracks.items [0] .id}”…? - Why do I get the error message : Uncaught TypeError: Unable to process binding “text: function (){return $data.tracks.items[0].id }”…? JavaScript 获取 response.status 或 response.text() 虽然不是两者 - JavaScript fetch response.status or response.text() though not both response.text() 中的额外引号 - Extra quotes in response.text() Fetch() Response.text() 未定义 - Fetch() Response.text() is undefined 反应为什么我在调用作为参数传递的 function 时收到错误“Uncaught TypeError: createTask is not a function”? - REACT Why do I get the error "Uncaught TypeError: createTask is not a function" when calling a function passed as a parameter? JAVASCRIPT-&gt;为什么会收到错误消息“ Uncaught TypeError:number not a function”? - JAVASCRIPT --> Why do I get the error message “Uncaught TypeError: number is not a function ”? 未捕获的TypeError:$(…).text不是函数 - Uncaught TypeError: $(…).text is not a function 当我尝试使用名为“dinle()”的 output 和 function 时出现错误。 错误代码是:未捕获的类型错误: - I get an error when I try to output the function with the name `dinle()`. The error code is:Uncaught TypeError: 如何使用 React 将 response.text() 转换为 JSON - How to convert response.text() to JSON with react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM