Unable to post to Restful API through Ajax using a

2019-06-11 12:59发布

I am developing Android application using PhoneGap. I am trying to execute simple login module, it works fine in web browser. But when it comes to apk, its not working. It is not able to post request to 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);
       }
  });

When I am executing this on android phone, I am getting only first alert above i.e "Before Calling Method" but it is not showing another alert. I have configured res/xml/cordova.xml as

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

Also I have referred some previous related doubts from StackOverflow. But It didn't helped me. Please help me to, I am waiting for positive response...

1条回答
小情绪 Triste *
2楼-- · 2019-06-11 14:04

try this one

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);
    }
});
查看更多
登录 后发表回答