[英]Execute a http request response in javascript
我有一个提供“JSON.stringify(data)”的 http 请求。
var xhr = new XMLHttpRequest();
xhr.open("GET", "/api/hello", true);
xhr.send();
xhr.onreadystatechange = function () {
console.log(xhr.responseText);
};
如何运行代码并打印数据内容?
您的代码应该可以正常工作,端点可能是问题,检查 url 您尝试从中进入端点,然后不要忘记检查 readyState 和您的请求的状态,然后什么都不做。
xhr.onreadystatechange = function () {
if (xhr.readState === 4 && xhr.status === 200)
{
console.log(xhr.responseText);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.