2017-04-18 90 views
0

我有一個應用程序使用Backbone.js,我使用Cordova構建此應用程序。該應用程序做了一個登錄請求,這在iOS和Web瀏覽器上正常工作,所以我知道該方法存在並正在工作。但是,對於Android,它返回一個「POST 404(未找到)」錯誤,即使代碼是相同的。爲什麼它在iOS上而不是在Android上運行?它會是代碼問題還是構建問題?Model.save在Android上引發404錯誤

這是我的要求:

return $.ajax(params); 

//params 
Object {headers: Object, type: "POST", dataType: "json", data: " 
{"email":"[email protected]","password":"password"}"…} 
crossDomain :true 
data :"{"email":"[email protected]","password":"password"}" 
dataType :"json" 
error :function (resp) 
headers: Object 
    Accept : "*/*; charset=utf-8" 
Content-Type :"application/json" 
isLoginRequest: true 
parse: true 
processData: false 
success: function (resp) 
type :"POST" 
url :"http://ip_address:port/api/login" 
validate: true 
__proto__ :Object 

//method 
var context, errors = [], model = new Model({}, {url: Config.login}); 
     model.set(data); 
     if(!model.isValid()){ 
      context = _.extend({ 
       errors: model.validationError 
      }, data); 
      this.renderLoginView(context); 
      return; 
     } 
     model.save(null, { 
      isLoginRequest: true, 
      success: _.bind(this.loginSuccess, this), 
      error: _.bind(function(xhr, textStatus){ 
       //this is where it goes 
       errors = {name: 'email/password', message: 'Invalid Username or Password'}; 
       context = _.extend({ 
        errors: errors 
       }, data); 
       this.renderLoginView(context); 
      },this) 
     }); 
+0

您確定您使用的IP是否正確?它是真實的IP地址還是本地地址? –

+0

這是正確的IP地址,相同的代碼在iOS上工作正常 –

+0

也許iOS連接到互聯網的另一種方式? –

回答

1

確保你已經安裝了科爾多瓦白名單插件。

cordova plugin add cordova-plugin-whitelist 

這對於Android而不是iOS或瀏覽器是必需的。

然後檢查你的config.xml中的這些線條。

<access origin="*"/> 

這將允許從您的應用程序的任何網絡通信。爲了更安全,您可以將其更改爲。

<access origin="YOUR_URL/*" /> 

這將允許網絡訪問只有指定的URL和任何子域名。

請參閱此處的文檔。

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/

+0

在這個世界上沒有任何話來表達我對你的感激之情,我一直在爲這個問題奮鬥了幾天。安裝該插件取得了訣竅!再次感謝 –

+1

沒問題我自己過去一直在與白名單問題鬥爭。樂意效勞。 –