科尔多瓦Android模拟器应用程序不调用http POST方法到RESTful php服务器?

问题描述:

我正在开发使用科尔多瓦在Ubuntu 14它是一种混合的应用程序,包括Android应用: - 服务器 - 使用PHP与纤薄框架&
客户端的RESTful API - 骨干与requirejs,jQuery的,引导等,HTML, CSS。科尔多瓦Android模拟器应用程序不调用http POST方法到RESTful php服务器?

我已经按照Apache Cordova文档指南(http://cordova.apache.org/docs/en/5.0.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide)中给出的步骤创建了该应用程序,并在android studio中导入了该应用程序。我正在使用android studio 1.3。

我已经使用(10.0.2.2)将应用程序连接到本地主机,该应用程序在模拟器上运行并显示“登录”屏幕。

challange在填写用户名和密码后,当我点击'Sign In'时,它应该触发http'POST',就像在浏览器应用程序中一样。但它不会触发POST,并且作为回报,我在Backbone.sync-error中收到404错误,并且当我看到服务器HTTP_METHOD时,它显示'GET'!

我覆盖了Backbone.sync方法。

这是我的“login.js”文件触发事件

//sigin button click code ... 
// ... 
signinInfo.set({ 
email: email, 
password: password  
}); 
signinInfo.save(null,{ 
success: function (data) {  
    window.localStorage.setItem('uid',signinInfo.attributes.uid);    
window.localStorage.setItem('email_id',signinInfo.attributes.email_id); 
// redirect the user to the given route      
if (data.attributes.status == "1") {       
    window.location.href = "";       
} else { 
    alert("Incorrect password!");      
} 
} // success 
}); 

上述“保存”上“signinInfo”的模式触发Backbone.sync方法。下面是来自models.js的代码片段,它覆盖“Backbone.sync”的方法:

originalSync = Backbone.sync; 
Backbone.sync = function (method, model, options) { 
    var success = options.success; 
    var error = options.error;   
    console.log("Models.js- method: " + method + ", model: " + JSON.stringify(model) + ", options: " + JSON.stringify(options)); 

    options.success = function (model, response, options) { 
     console.log("Models.js- success, response: " +response); 
     $('.srLoading').hide(); 
     if (typeof model.redirect == 'undefined') { 
      success(model, response, options); 
     } else { 
      window.location.replace("/"); 
     } 
    }; 
    options.error = function (model, response, options) { 
     console.log("Models.js- error:" +JSON.stringify(model) + " response: " + response + "; options: " + JSON.stringify(options)); 
     $('.srLoading').hide(); 
     error(model, response, options); 
    }; 
    // I have tried to put options for crossDomain here, but its not working 
    options = options || (options = {}); 

    if (!options.crossDomain) { 
     options.crossDomain = true; 
    } 

    if (!options.xhrFields) { 
     options.xhrFields = {withCredentials:true}; 
    } 
    if (method === "read") { 
     console.log("Models.js- read method!"); 
     $('.srLoading').show(); 
     options.dataType = "jsonp"; 
     return originalSync.apply(Backbone, arguments); 
    } 

    if (method === "create") { 
     console.log("Models.js- create method!"); 
     $('.srLoading').show(); 
     options.dataType = "jsonp"; 

     options.contentType = 'application/json'; 
     options.type = 'POST'; 
     //options.data = JSON.stringify(options.data); 

     return originalSync.apply(Backbone, arguments); 
    } 

    if (method === "update") { 
     $('.srLoading').show(); 
     options.dataType = "jsonp"; 
     return originalSync.apply(Backbone, arguments); 
    } 
    if (method === "delete") { 
     $('.srLoading').show(); 
     options.dataType = "jsonp"; 
     return originalSync.apply(Backbone, arguments); 
    } 
}; //Backbone.sync 

以上,方法“创造”的调用,但在服务器它不会转换为“POST”请求。相反,$ _SERVER ['REQUEST_METHOD']显示'GET'! :(

+0

后的代码片段应使'POST'请求。 – Breavyn

以我Backbone.sync方法我评论[options.dataType = “JSONP”],以便代码如下所示:

... 
if (method === "create") { 
    console.log("Models.js- create method!"); 
    $('.srLoading').show(); 
    //options.dataType = "jsonp"; 
    options.contentType = 'application/json'; 
    return originalSync.apply(Backbone, arguments); 
} 

现在我的登录发送HTTP POST请求发送到服务器!

跨域(CORS),骨干带的dataType“JSONP”只能使“GET”的要求。因此,为了使我们需要发送“JSON”数据的其他行为。