我尝试向远程服务器中的API发送请求。
$.ajax({
type: "POST",
url: "http://myserverIPAddress/myAPIPath.aspx/FunctionName",
data: "{myID :7 }",
contentType: "application/json; charset=utf-8;",
dataType: "json",
success: function (response) {
debugger;
},
error: function (response, res, r) {
debugger;
}
});
请求失败,我收到此错误:
无法加载资源:服务器响应状态为404(未找到) XMLHttpRequest无法加载http://myserverIPAddress/myAPIPath.aspx/FunctionName。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://localhost:5316”访问。响应的HTTP状态代码为404.
感谢您的帮助。
答案 0 :(得分:0)
你被同源政策所击中。基本上发生的事情是远程服务器没有在您请求的内容上设置正确的标头,您无法在页面中加载它。要从远程服务器加载数据(称为跨源请求 - CORS),服务器运营商将需要添加" Access-Control-Allow-Origin"标题为他们的内容。更多信息here.