从本地主机到同一wifi上的另一台服务器的Ajax请求

Ajax request from localhost to another server on the same wifi

本文关键字:一台 请求 Ajax 服务器 主机 wifi      更新时间:2023-09-26

实际上,在不同的场景下,这里有几个问题。

所以我连接到了RESTful服务器所在的同一个wifi。

我在IntelliJ中打开了一个网页,它在我的机器上创建了一个本地主机服务器,这样它就可以启动我的网络摄像头。(当我试图从我的文件目录打开网页时,网络摄像头不会启动)

我正在尝试执行一个ajax请求,它看起来像这样。

$.ajax({url:'http://192.xxx.x.xx:xxxx/restful/webapi/enroll',//url:'http://localhost:63xxx/example-1.0.0.0-样本/',类型:"PUT",contentType:"application/json",processData:false,数据:数据,dataType:'json',crossDomain:true,错误:函数(xhr,状态){if(xhr.status!="201"){//201表示已创建,而不是200表示成功$("#statusEnrollmentMsg").text("失败"+状态);console.log(错误);}其他{$("#textbox").text("success");}},成功:函数(结果){$("#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/

crossDomain(默认值:对于相同域请求为false,对于跨域请求)

类型:布尔

如果您希望在domain,将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");
            }
        });