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

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

我有一个模拟服务器,它用这样的正文文本进行响应:“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>

在浏览器控制台中,我看到了这个错误,并且我的 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 是异步的,所以当您调用 response.text() 时,您实际上还没有响应。使用 response = await fetch 或回调。

这是对我有用的脚本:

<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>