我正在開發使用科爾多瓦在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'! :(
後的代碼片段應使'POST'請求。 – Breavyn