简体   繁体   English

在javascript中使用跨源的http post请求

[英]http post request with cross-origin in javascript

i have a problem with a http post call in firefox. 我在firefox中遇到http post调用问题。 I know that when there are a cross origin, firefox first do a OPTIONS before the POST to know the access-control-allow headers. 我知道当有一个交叉起源时,firefox首先在POST之前执行OPTIONS以了解access-control-allow标头。 With this code i dont have any problem: 使用此代码我没有任何问题:

Net.requestSpeech.prototype.post = function(url, data) {
    if(this.xhr != null) {
        this.xhr.open("POST", url);
        this.xhr.onreadystatechange = Net.requestSpeech.eventFunction;
        this.xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        this.xhr.send(data);
    }
}

I test this code with a simple html that invokes this function. 我用一个调用这个函数的简单html来测试这段代码。 Everything is ok and i have the response of the OPTIONS and POST, and i process the response. 一切都很好,我有OPTIONS和POST的响应,我处理响应。 But, i'm trying to integrate this code with an existen application with uses jquery (i dont know if this is a problem), when the send(data) executes in this case, the browser (firefox) do the same, first do a OPTION request, but in this case dont receive the response of the server and puts this message in console: 但是,我正在尝试将此代码与使用jquery的existen应用程序集成(我不知道这是否有问题),当在这种情况下执行send(data)时,浏览器(firefox)也这样做,首先要做一个OPTION请求,但在这种情况下,不要收到服务器的响应并将此消息放入控制台:

[18:48:13.529] OPTIONS http://localhost:8111/ [undefined 31ms]

Undefined... the undefined is because dont receive the response, but the code is the same, i dont know why in this case the option dont receive the response, someone have an idea? 未定义...未定义是因为没有收到响应,但代码是相同的,我不知道为什么在这种情况下选项没有收到响应,有人有想法?

i debug my server app and the OPTIONS arrive ok to the server, but it seems like the browser dont wait to the response. 我调试我的服务器应用程序和OPTIONS到服务器,但似乎浏览器不等待响应。

edit more later: ok i think that the problem is when i run with a simple html with a SCRIPT tag that invokes the method who do the request run ok, but in this app that dont receive the response, i have a form that do a onsubmit event, i think that the submit event returns very fast and the browser dont have time to get the OPTIONS request. 稍后编辑:好吧我认为问题是当我使用带有SCRIPT标签的简单html运行时调用执行请求的方法运行正常,但是在这个没有收到响应的应用程序中,我有一个表单可以执行onsubmit事件,我认为提交事件返回非常快,浏览器没有时间来获取OPTIONS请求。

edit more later later: WTF, i resolve the problem make the POST request to sync: 稍后再编辑:WTF,我解决问题使POST请求同步:

this.xhr.open("POST", url, false);

The submit reponse very quickly and can't wait to the OPTION response of the browser, any idea to this? 提交响应非常快,不能等到浏览器的OPTION响应,有什么想法吗?

由于相同的原始政策,您不能发送交叉原始帖子,您可以通过在iframe中包含网站(如果有访问域名)原始网站包含iframe到外部网站来解决它,内部方向是合法的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM