从本地到另一台服务器的本地主机上的Ajax请求在相同的wifi上
问题描述:
实际上,在不同情况下,这里有几个问题。从本地到另一台服务器的本地主机上的Ajax请求在相同的wifi上
所以我连接到一个RESTful服务器托管相同的WiFi。
我在IntelliJ中打开了一个网页,它在我的机器上创建了一个localhost服务器,以便它可以启动我的摄像头。 (当我尝试从我的文件目录中打开网页时,网络摄像头无法启动)
我试图做一个像这样的ajax请求。
$.ajax({ url: 'http://192.xxx.x.xx:xxxx/restful/webapi/enroll', //url: 'http://localhost:63xxx/example-1.0.0.0-samples/', type: 'PUT', contentType: "application/json", processData: false, data: data, dataType: 'json', crossDomain:true, error: function (xhr, status) { if(xhr.status != "201"){ // 201 means it is Created, rather than 200 which means Success $("#statusEnrollmentMsg").text("Fail " + status); console.log(error); }else{ $("#textbox").text("success"); } }, success: function (result) { $("#textbox").text("success"); } });
错误代码
jquery.min.js:4 OPTIONS http://192.xxx.x.xx:xxxx/restful/webapi/enroll
send @ jquery.min.js:4
ajax @ jquery.min.js:4
OnEnroll @ example.js:130
onclick @ example.html:54
example.html:1
XMLHttpRequest cannot load http://192.xxx.x.xx:xxxx/restful/webapi/enroll. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:63xxx' is therefore not allowed access. The response had HTTP status code 404.
我总是得到错误,看起来像这样。 如果不清楚善意评论,我会做出调整。
跨域已被设置为true!
我想发送一些json对象到这个服务器。我不确定这是否也是适当的协议。请指教。
答
检查文档:http://api.jquery.com/jQuery.ajax/
跨域(默认为false同域的要求,真正为 跨域请求)
类型:Boolean
如果要强制跨域请求(如JSONP)在同一个 域中,将crossDomain的值设置为true。例如,对于 ,可以将服务器端重定向到另一个域。 (版本补充说: 1.5)
你的代码应该是这样的
$.ajax({
url: 'http://192.xxx.x.xx:xxxx/restful/webapi/enroll',
type: 'PUT',
crossDomain: true,
contentType: "application/json",
processData: false,
data: data,
dataType: 'json',
crossDomain:true,
error: function (xhr, status) {
if(xhr.status != "201"){ // 201 means it is Created, rather than 200 which means Success
$("#statusEnrollmentMsg").text("Fail " + status);
console.log(error);
}else{
$("#textbox").text("success");
}
},
success: function (result) {
$("#textbox").text("success");
}
});
其中跨域已经被设置为true
!你的代码和我的区别是什么?我没有看到它,对不起! –
Ohh对不起,尝试使用GET/POST方法,如果它的工作,那么你必须在AJAX请求中修改标题。 –
对不起,什么是标题?我尝试了一个POST请求,我回来了同样的错误 –