2015-02-11 111 views
3

我正在使用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"); 
       } 
      } 
     ) 

希望您能幫助我找到一個解決方案。

+0

'jQuery.support.cors = true;' - 爲什麼你重寫jQuery的cors檢測?如果瀏覽器不支持cors,那麼告訴jQuery它不會對任何事情有所幫助。 – Quentin 2015-02-12 17:09:42

+1

'Access-Control-Allow- *'是*響應*標頭,由服務器發送*。在您的請求中發送這些標題可能會破壞您的請求。儘管如此,我對Cordova的跨源性行爲的經驗有限,所以我不太確定。 – apsillers 2015-02-12 17:10:14

+0

當我遇到問題時,我添加了Jquery.support.cors和3最後一個setRequestHeader。之前沒有工作。 – 2015-02-13 11:57:56

回答

-1

jQuery documentation狀態:

在beforeSend函數返回false將取消該請求

由於您beforeSend函數沒有return語句將返回undefined這是falsy。我假設取消你的請求(即使我沒有測試它)。嘗試在beforeSend函數結尾處返回true

+0

但它在調試模式下工作正常......您所描述的問題必須導致這兩種情況下的問題。 – 2015-02-13 11:58:54

+0

的確如此。我應該仔細閱讀:-)希望你能找到解決方案。 – jonas 2015-02-16 12:03:47

+0

謝謝,我也希望。我會做,如你所說,它可能會避免未來可能出現的錯誤。 – 2015-02-16 15:22:13