問(wèn)題描述
我正在通過(guò) jQuery 的 $.ajax 函數(shù)在我一直使用的第三方 API 上調(diào)用 POST.但是,當(dāng)我撥打電話時(shí),我收到以下錯(cuò)誤:XMLHttpRequest 無(wú)法加載 http://the-url.com.該請(qǐng)求被重定向到http://the-url.com/anotherlocation",這對(duì)于需要預(yù)檢的跨域請(qǐng)求是不允許的.
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.
我從 這篇文章中看到 這可能是一個(gè) Webkit 錯(cuò)誤,所以我在 Firefox 中進(jìn)行了嘗試(我正在 Chrome 中開(kāi)發(fā)),我得到了相同的結(jié)果.我在 Chrome 和 Firefox 上嘗試過(guò),我得到了相同的結(jié)果.
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.
根據(jù) 這篇文章,我還嘗試通過(guò)設(shè)置 crossDomain 來(lái)使用 jsonp$.ajax 函數(shù)的
屬性為 true
并將 dataType
設(shè)置為 jsonp
.但是,這導(dǎo)致了 500 內(nèi)部服務(wù)器錯(cuò)誤.
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.
當(dāng)我使用 --disable-web-security 標(biāo)志啟動(dòng) Chrome 時(shí),我沒(méi)有任何問(wèn)題.但是,如果我正常啟動(dòng)瀏覽器,則會(huì)收到錯(cuò)誤消息.
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.
所以,我想這可能是一個(gè)由兩部分組成的問(wèn)題.我該怎么做才能發(fā)出這個(gè)跨域請(qǐng)求?如果 JSONP 是答案,那么我該如何確定第三方 API 是否設(shè)置正確以支持此功能?
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?
這是我在禁用瀏覽器安全性的情況下?lián)艽螂娫挄r(shí)的屏幕截圖: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
這是我在啟用瀏覽器安全性的情況下(正常情況下)撥打電話時(shí)的屏幕截圖: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
推薦答案
我想出的解決方案是使用 cURL(正如 @waki 所提到的),但稍微修改了一個(gè)支持 SOAP 的版本.然后,我沒(méi)有對(duì)第三方 API 進(jìn)行 AJAX 調(diào)用(配置不正確),而是調(diào)用我的本地 PHP 文件,該文件然后對(duì)第三方 API 進(jìn)行 SOAP 調(diào)用并將數(shù)據(jù)傳遞回我的 PHP 文件,我可以然后處理它.這讓我忘記了 CORS 以及與之相關(guān)的所有復(fù)雜性.這是代碼(取自this問(wèn)題,但沒(méi)有經(jīng)過(guò)身份驗(yàn)證).
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)
這篇關(guān)于跨域 AJAX 請(qǐng)求不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!