无法使用Android手机通过Ajax发布到Restful API

时间:2014-03-21 10:50:09

标签: android ajax rest cordova

我正在使用PhoneGap开发Android应用程序。我正在尝试执行简单的登录模块,它在Web浏览器中工作正常。但是当谈到apk时,它不起作用。它无法向Restful API发布请求。

var usr = $('#email_address').val();
var pass = $('#password').val();
if(usr != '' && pass != ''){
   alert("Before Calling Method");                      
   $.ajax({
       url:APIURL +"api/sample/sample123",
       type: "POST",
       crossDomain : true,
       data: {username:usr,password:pass},
       dataType: "json",
       success: function(data){    /*Here I am posting some dummy code, I can't show original code*/
           var response = eval(data);
           alert(response.response);
       }
  });

当我在Android手机上执行此操作时,我只获得上面的第一个警告,即“在调用方法之前”,但它没有显示另一个警报。我已将res / xml / cordova.xml配置为

<access uri="*" subdomains="true"/>

此外,我还提到了StackOverflow以前的一些相关疑问。但它没有帮助我。请帮助我,我正在等待积极回应...

1 个答案:

答案 0 :(得分:1)

尝试这个

formData = {
    username: $('#email_address').val(),
    password: $('#password').val()
}
$.ajax({
    url: APIURL + "api/sample/sample123",
    type: "POST",
    crossDomain: true,
    contentType: 'application/json',
    data: formData,
    dataType: "json",
    success: function(data) { 
        alert(data);
    }
});