在javascript中执行http请求响应

时间:2020-04-19 21:57:58

标签: javascript json xml http request

我有一个http请求,其中传递了“ JSON.stringify(data)”。

var xhr = new XMLHttpRequest();
xhr.open("GET", "/api/hello", true);
xhr.send(); 
xhr.onreadystatechange = function () {
    console.log(xhr.responseText);
};

如何运行代码并打印数据内容?

1 个答案:

答案 0 :(得分:0)

您的代码应该正常工作,可能是端点问题,请检查您尝试从中进入端点的URL,然后别无所事地检查一下readyState和请求状态。

xhr.onreadystatechange = function () {
    if (xhr.readState === 4 && xhr.status === 200)
    {
       console.log(xhr.responseText);
    }
};
相关问题