【问题标题】:Ajax request from localhost to another server on the same wifi从本地主机到同一 wifi 上的另一台服务器的 Ajax 请求
【发布时间】:2016-07-29 09:19:26
【问题描述】:

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

所以我连接到与托管 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/',
            类型:'PUT',
            内容类型:“应用程序/json”,
            处理数据:假,
            数据:数据,
            数据类型:'json',
            跨域:真,
            错误:函数(xhr,状态){
                if(xhr.status != "201"){ // 201 表示已创建,而不是 200 表示成功
                    $("#statusEnrollmentMsg").text("失败" + 状态);
                    控制台日志(错误);
                }别的{
                    $("#textbox").text("成功");
                }
            },
            成功:函数(结果){
                $("#textbox").text("成功");
            }
        });

错误代码

    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.

我总是收到类似这样的错误。 如果不清楚请评论,我会做出调整。

跨域已经设置为真了!

我想向这个服务器发送一些 json 对象。我不确定这是否也是正确的协议。请指教。

【问题讨论】:

    标签: javascript jquery ajax rest intellij-idea


    【解决方案1】:

    查看文档:http://api.jquery.com/jQuery.ajax/

    crossDomain (默认: false 表示同域请求,true 表示 跨域请求)

    类型:布尔值

    如果您希望在同一服务器上强制执行跨域请求(例如 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");
                }
            });
    

    【讨论】:

    • 跨域已经设置为真了!你的代码和我的有什么区别?我没看到,对不起!
    • 哦,对不起,尝试使用 GET/POST 方法,如果它有效,那么您必须修改 AJAX 请求中的标头。
    • 对不起,什么是标题?我尝试了一个 POST 请求,但我得到了同样的错误
    • 您好如何修改表头?
    猜你喜欢
    • 2020-12-18
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多