Cordova Android 模拟器应用程序未调用 RESTful php 服务器的 http POST 方法?

Cordova Android emulator app not invoking http POST method to the RESTful php server?

我正在 android 上使用 cordova 在 Ubuntu 14 上开发应用程序。它是一个混合应用程序,包括:- 服务器- RESTful api using Php with slim framework &
客户端 - Backbone with requirejs,jquery,bootstrap 等.., HTML,CSS.

我已按照 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) 将应用程序连接到我的本地主机,该应用程序在模拟器上运行并显示 'login' 屏幕。

挑战是,在填写用户名和密码后,当我单击 'Sign In' 时,它应该像在浏览器应用程序中一样触发 http 'POST'。但它不会触发 POST 并且在 return 中我在 Backbone.sync 中收到 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'模型上面的'save'触发了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

上面,方法 'create' 被调用,但在服务器上它没有转换为 'POST' 请求。相反 $_SERVER['REQUEST_METHOD'] 显示 'GET'! :(

在我的 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) 上,数据类型 'jsonp' 的 backbone 只能发出 'GET' 请求。所以要进行其他操作,我们需要发送 'json' 数据。