問題描述
我正在通過 jQuery 的 $.ajax 函數在我一直使用的第三方 API 上調用 POST.但是,當我撥打電話時,我收到以下錯誤:XMLHttpRequest 無法加載 http://the-url.com.該請求被重定向到http://the-url.com/anotherlocation",這對于需要預檢的跨域請求是不允許的.
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.
我從 這篇文章中看到 這可能是一個 Webkit 錯誤,所以我在 Firefox 中進行了嘗試(我正在 Chrome 中開發),我得到了相同的結果.我在 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.
根據 這篇文章,我還嘗試通過設置 crossDomain 來使用 jsonp$.ajax 函數的
屬性為 true
并將 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.
當我使用 --disable-web-security 標志啟動 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?
這是我在禁用瀏覽器安全性的情況下撥打電話時的屏幕截圖: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
這是我在啟用瀏覽器安全性的情況下(正常情況下)撥打電話時的屏幕截圖: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 所提到的),但稍微修改了一個支持 SOAP 的版本.然后,我沒有對第三方 API 進行 AJAX 調用(配置不正確),而是調用我的本地 PHP 文件,該文件然后對第三方 API 進行 SOAP 調用并將數據傳遞回我的 PHP 文件,我可以然后處理它.這讓我忘記了 CORS 以及與之相關的所有復雜性.這是代碼(取自this問題,但沒有經過身份驗證).
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 請求不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!