跨域AJAX请求不工作 [英] Cross-domain AJAX request not working

查看:104
本文介绍了跨域AJAX请求不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话POST的第三方API,我一直在努力通过jQuery的$就功能。然而,当我打这个电话,我得到了以下错误: XMLHtt prequest无法加载http://the-url.com。该请求被重定向到'http://the-url.com/anotherlocation',这是不允许的,需要preflight跨域请求。

I'm calling POST on a third-party API that I've been working with via jQuery's $.ajax function. However, when I make the call I get the following error: XMLHttpRequest cannot load http://the-url.com. The request was redirected to 'http://the-url.com/anotherlocation', which is disallowed for cross-origin requests that require preflight.

我从<一看见href="http://stackoverflow.com/questions/20229248/which-is-disallowed-for-cross-origin-requests-that-require-$p$pflight-error-in">this帖子,这可能是一个错误的Webkit,所以我想它在Firefox(我开发的Chrome浏览器),我得到了相同的result.I've尝试这样的Chrome和Firefox,我也得到了同样的结果。

I saw from this post that this might be a Webkit bug, so I tried it in Firefox (I'm developing in Chrome) and I got the same result.I've tried this on Chrome and Firefox and I get the same result.

这个帖子,我还试图通过设置<$ C $使用JSONP两个C>跨域的$就功能财产并设置的dataType JSONP 。但是,这造成了500内部服务器错误。

Per this post, I also tried using jsonp both by setting the crossDomain property of the $.ajax function to true and setting the dataType to jsonp. But, this caused a 500 internal server error.

当我开始使用Chrome浏览器的 - 禁用网络安全标志,我没有任何问题。不过,如果我启动浏览器正常,然后我得到的错误。

When I start Chrome with the --disable-web-security flag, I don't have any problems. However, if I start the browser normally, then I get the error.

所以,我想那种这可能是一个两部分的问题。我能做些什么,使这个跨域请求?如果JSONP是答案,那我怎么去搞清楚,如果第三方API设置正确支持此?

So, I guess this might sort of be a 2-part question. What can I do to make this cross-domain request? If JSONP is the answer, then how do I go about figuring out if the third-party API is set up correctly to support this?

编辑:的下面是截图当我提出与禁用浏览器的安全呼叫:<一href="https://drive.google.com/file/d/0Bzo7loNBQcmjUjk5YWNWLXM2SVE/edit?usp=sharing">https://drive.google.com/file/d/0Bzo7loNBQcmjUjk5YWNWLXM2SVE/edit?usp=sharing

Here's the screenshot when I make the call with the browser security disabled: https://drive.google.com/file/d/0Bzo7loNBQcmjUjk5YWNWLXM2SVE/edit?usp=sharing

下面是screenchost当我提出启用(像正常)浏览器的安全呼叫:<一href="https://drive.google.com/file/d/0Bzo7loNBQcmjam5NQ3BKWUluRE0/edit?usp=sharing">https://drive.google.com/file/d/0Bzo7loNBQcmjam5NQ3BKWUluRE0/edit?usp=sharing

Here's the screenchost when I make the call with the browser security enabled (like normal): https://drive.google.com/file/d/0Bzo7loNBQcmjam5NQ3BKWUluRE0/edit?usp=sharing

推荐答案

这是我想出了是使用卷曲的解决方案(如@waki提到的),但支持SOAP略加修改。而不是使AJAX调用第三方API(其配置不正确)。然后,我打这个电话给我本地的PHP文件,然后让一个SOAP调用第三方API并将数据传回我的PHP文件,在那里我可以然后对其进行处理。这使我忘记CORS和所有与之相关联的复杂性。这里的code(采取从修改这个的问题,但没有身份验证)。

The solution that I came up with was to use cURL (as @waki mentioned), but a slightly modified version that supports SOAP. Then, instead of making the AJAX call to the third party API (which is configured incorrectly) I make the call to my local PHP file which then makes a SOAP call to third party API and passes the data back to my PHP file where I can then process it. This allows me to forget about CORS and all of the complexities associated with it. Here's the code (taken and modified from this question, but without the authentication).

$post_data = "Some xml here";
$soapUrl = "http://yoursite.com/soap.asmx"; // asmx URL of WSDL


$headers = array(
    "Content-type: text/xml;charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "SOAPAction: http://yoursite.com/SOAPAction",
    "Content-length: " . strlen($post_data),
); //SOAPAction: your op URL

$url = $soapUrl;

// PHP cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

/* Check for an error when processing the request. */
if(curl_errno($ch) != 0) {
   // TODO handle the error
}

curl_close($ch);

// TODO Parse and process the $response variable (returned as XML)

这篇关于跨域AJAX请求不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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