在javascript中使用跨源的http post请求 [英] http post request with cross-origin in javascript

查看:95
本文介绍了在javascript中使用跨源的http post请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在firefox中遇到http post调用问题。我知道当有一个交叉起源时,firefox首先在POST之前执行OPTIONS以了解access-control-allow标头。
使用此代码,我没有任何问题:

i have a problem with a http post call in firefox. I know that when there are a cross origin, firefox first do a OPTIONS before the POST to know the access-control-allow headers. 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);
    }
}

我用一个调用此代码的简单html测试此代码功能。
一切都很好,我有OPTIONS和POST的响应,我处理响应。但是,我正在尝试将此代码与使用jquery的existen应用程序集成(我不知道这是否有问题),当在这种情况下执行send(data)时,浏览器(firefox)也这样做,首先要做一个OPTION请求,但在这种情况下,不接收服务器的响应并将此消息放入控制台:

I test this code with a simple html that invokes this function. Everything is ok and i have the response of the OPTIONS and POST, and i process the response. 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:

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

未定义...... undefined是因为没有收到回复,但代码是一样的,我不知道为什么在这种情况下选项没有收到回复,有人有想法?

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调试我的服务器应用程序和OPTIONS到服务器,但似乎浏览器不等待响应。

i debug my server app and the OPTIONS arrive ok to the server, but it seems like the browser dont wait to the response.

稍后编辑:好吧我认为问题是当我用一个带有SCRIPT标签的简单html运行时调用执行请求的方法运行正常,但是在这个没有收到响应的应用程序中,我有一个表单可以执行操作ubmit事件,我认为提交事件返回非常快,浏览器没有时间来获取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.

稍后再编辑:WTF,我解决问题使POST请求同步:

edit more later later: WTF, i resolve the problem make the POST request to sync:

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

提交响应非常快,不能等到浏览器的OPTION响应,任何想法这个?

The submit reponse very quickly and can't wait to the OPTION response of the browser, any idea to this?

推荐答案

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

Due to the same origin policy, you can't send cross origin post, you can workaround it by include sites in iframes (if have access to the domain) original site contains iframe to the outer site, the inner direction is legal.

这篇关于在javascript中使用跨源的http post请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆