2017-04-18 91 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

没问题我自己过去一直在与白名单问题斗争。乐意效劳。 –