【发布时间】:2017-06-18 04:08:43
【问题描述】:
我的 Ajax 请求没有收到正确的响应!它总是进入readyState '4',响应状态为'0' ??
客户代码:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4 && xhttp.status == 200){
document.getElementById("lastNameLabel").innerHTML = "response recieved!";
const responseJSON = JSON.parse(xhttp.responseText);
if(responseJSON.emailTaken == "false"){
window.location = server+"/client/home.html";
}
}
};
xhttp.open("POST", server+"/signUp", true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send("email="+email);
服务器代码(Node.js + Express):
app.post('/signUp', (req, res) => {
res.end("it worked!!");
});
我知道我的服务器正在处理请求,因为我看到了 url 调用,但响应从未到达 html 客户端!!。我被卡住了!
【问题讨论】:
-
这是一个跨源请求 - 检查浏览器开发人员工具控制台,看看您是否有任何关于 cors 的错误
-
这是什么意思??
-
表示检查浏览器developers工具控制台是否有错误
-
不知道存在!!我看到了:MLHttpRequest 无法加载 localhost:8080/signUp。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 不允许访问。修复了问题!!谢谢!!!!!!
-
你去吧,这是一个跨源资源共享错误 - 你需要将 CORS 处理添加到你发送请求的服务器 - 有一个 CORS 库,你可以简单地插入到你的代码,这样你就不必考虑太多
标签: javascript node.js ajax express