我正在从script.js文件进行API调用。当我向chatbot发送消息时,它在“检查”->“控制台”和“检查->网络”选项卡中给了我两个不同的响应。


  网络标签


javascript - 为什么在浏览器控制台和“网络”选项卡中得到两种不同的响应-LMLPHP


  控制台标签


javascript - 为什么在浏览器控制台和“网络”选项卡中得到两种不同的响应-LMLPHP


  script.js


fetch(`${url}/conversations/default/respond`, {
        mode: 'no-cors',
        method: 'POST',
        // dataType:'jsonp',
        body: JSON.stringify(data),
        headers: {
          'Content-Type': 'application/json',
        },
    })
        .then(function (response) {
            console.log("RESPONSE",response);
            if (response) {
                for (let response of response) {
                console.log(response.text);
                    createResponder(response.text);
            }
            } else {
                createResponder("Sorry, I'm having trouble understanding you, try asking me in an other way")
            }
        })
        .catch(function (err) {
            //console.log("DIFF",err);
            document.getElementById('typing').style.display = "none";
            createResponder("I'm having some technical issues. Try again later :)");
        });

最佳答案

网络标签包含请求寿命,开始,响应状态,标头,数据等的所有数据...

在这一部分上:

.then(function (response) {
            console.log("RESPONSE",response);
            if (response) {
                for (let response of response) {
                console.log(response.text);
                    createResponder(response.text);
            }
            } else {
                createResponder("Sorry, I'm having trouble understanding you, try asking me in an other way")
            }
        })


您正在控制台记录xhr对象本身,这就是为什么您的控制台包含“请求”数据而不是json数据的原因,我想这是您所期望的。

继续阅读this并注意“响应对象”部分

关于javascript - 为什么在浏览器控制台和“网络”选项卡中得到两种不同的响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56147552/

10-12 12:21
查看更多