我正在使用ajax的科爾多瓦應用程序。我的問題是,在調試中,應用程序正在工作。但當我建立一個版本,我得到了錯誤:未能執行'發送'XMLHttpRequest'
{"readyState":0,"status":0,"statusText":"NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://sub.domain.tld'."}
我不明白從哪裏來。 我添加所需行至RES/XML/config.xml中
<access origin="*" />
<access uri="*" subdomains="true" />
<feature name="http://api.phonegap.com/1.0/network"/>
而且在AndroidManifest許可INTERNET。但錯誤仍在進行之中......
呵呵,這是我的Ajax調用:
jQuery.support.cors = true;
$.ajax(
{
accepts: {
xml: "text/xml; charset=ISO-8859-1",
text: "text/plain; charset=ISO-8859-1"
}
, converters: {
"text xml": jQuery.parseXML
}
, type: "POST"
//, contentType: "text/xml; charset=ISO-8859-1"
, contentType : "application/x-www-form-urlencoded; charset=iso-8859-1"
, url: address
, async: false
, cache: false
, crossDomain: false
, data: msg
, dataType: "text"
, global: false
, ifModified: false
, username: this.params.server.login
, password: this.params.server.pass
, timeout: 500000
, beforeSend: function(jqXHR) {
jqXHR.overrideMimeType('text/xml;charset=iso-8859-1');
jqXHR.setRequestHeader("Access-Control-Allow-Origin", "*");
jqXHR.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
jqXHR.setRequestHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE, PUT, HEAD");
}
}
)
希望您能幫助我找到一個解決方案。
'jQuery.support.cors = true;' - 爲什麼你重寫jQuery的cors檢測?如果瀏覽器不支持cors,那麼告訴jQuery它不會對任何事情有所幫助。 – Quentin 2015-02-12 17:09:42
'Access-Control-Allow- *'是*響應*標頭,由服務器發送*。在您的請求中發送這些標題可能會破壞您的請求。儘管如此,我對Cordova的跨源性行爲的經驗有限,所以我不太確定。 – apsillers 2015-02-12 17:10:14
當我遇到問題時,我添加了Jquery.support.cors和3最後一個setRequestHeader。之前沒有工作。 – 2015-02-13 11:57:56