我有以下的情況。GET子域的HTML頁面的jQuery
從位於應用程序https://subdomain.mydomain.com我試圖讓位於https://mydomain.com/page.aspx
我一個頁面的HTML 「M與jQuery的$就方法這樣做。
$.ajax({
url: url,
type: 'GET',
contentType: 'application/x-www-form-urlencoded',
success: function (data, status, xhr) {
console.info("success");
},
error: function (xhr, status, error) {
console.error(error);
}
});
ASPX頁面實際上是調用和執行。我看到響應狀態是200 OK並且它具有預期的大小(80KB),但響應內容(80KB)是空的。 jQuery的錯誤也不是那麼有用:
readyState: 0,
responseText: ""
status: 0,
statusText: "error"
我想這是涉及同源策略的東西。我看到人們用YQL解決這類問題(但我不想使用額外的庫)和JSONP(但我使用純HTML)。
在絕望的時刻我也嘗試的技巧,比如
document.domain = 'mydomain.com';
jQuery.support.cors = true;
沒有成功。任何提示?
的問題是由於必須對所謂的應用程序啓用了Access-Control-Allow-Origin
。在我的情況下,它是IIS7託管中的一個asp.net應用程序。我不得不在web.config中添加這些行:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
以下網站報道,許多配置enable CORS在許多服務器類型
工作正常!我只需要添加到正確的服務器配置。此鏈接幫助了我很多 http://enable-cors.org/ 感謝您的幫助! – abx78 2012-04-24 20:12:58