【发布时间】:2016-12-09 11:10:20
【问题描述】:
我正在尝试使用 ajax 发送发布请求,但我不断收到以下错误:
XMLHttpRequest 无法加载 http://192.168.1.123:8080。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:8080' 因此不允许访问。
这是我的代码
$.ajax({
type: "POST",
url: "http://192.168.1.123:8080",
data: JSON.stringify([{"VisitorName ": " "+document.getElementById("VisitorName ").value}
]),
contentType: "application / json ",
crossDomain: true,
dataType: "json",
success: function (data) {
alert(data);
},
failure: function (errMsg) {
alert(errMsg);
}
});
【问题讨论】:
-
这是 CORS 错误,服务器不允许除同一域之外的任何其他客户端进行请求,请参阅此以获取更多信息stackoverflow.com/questions/25923796/cors-error-with-jquery
-
您的代码也有一些语法错误。就像在
url上一样,您不要关闭"、,在data等代码中抛出的,。
标签: javascript jquery ajax post http-post