2

I'm developing a mobile application based on PhoneGap. I need to use AJAX, I'm working with jQuery.

I'm trying to get a JSON from a WS on PHP with CORS, but I get an error, and the error message is empty. I tested the code in a Web Browser and it works. But, when I used it with PhoneGap it did not worked. I tested my code in PhoneGAp with Fecebook WS and it works. This is the combination that I tested:

Test 1
Source: file:///C:/.../index.html (Web Browser)
WebService: http: //localhost/.../getemployees.php
Result: it works

Test 2
Source: PhoneGap
WebService: http: //localhost/.../getemployees.php
Result: it does not works

Test 3
Source: PhoneGap
WebService: https: //graph.facebook.com/OldemarshCr
Result: it works

This is the code:

$.getJSON(url, function (data) {console.log(data);})
.success(function() { console.log("second success"); })
.error(function(jqXHR, textStatus, errorThrown) {
console.log('******* '+"error: " + textStatus+' *******');
});

This is the JSON response:

{"items":[{"id":"10","firstName":"Kathleen","lastName":"Byrne","title":"Sales Representative","picture":"kathleen_byrne.jpg","reportCount":"0"},{"id":"9","firstName":"Gary","lastName":"Donovan","title":"Marketing","picture":"gary_donovan.jpg","reportCount":"0"},{"id":"7","firstName":"Paula","lastName":"Gates","title":"Software Architect","picture":"paula_gates.jpg","reportCount":"0"]}

Thanks,

1
  • do you use andoid emulator ? Aug 22, 2013 at 8:42

3 Answers 3

0

In ajax call, try and add

         $.ajax({
                crossDomain: true,                 
                xhrFields: {withCredentials: true}
              });
1
  • I'm not using the ajax method, but I will try with it Jun 18, 2013 at 4:55
0

$.ajax did not work... this is the error that I get

{"readyState":0,"responseText":"","status":0,"statusText":"error"}

I tested the code on web browser and it worked.

This is the code:

$.ajax({
  crossDomain: true,  
  xhrFields: {withCredentials: true},
  type: 'GET', 
  url: url, 
  dataType: 'json', 
  success: function(response){ console.log(response); }
  error: function(error){ console.log('Error: '+error); }
});

Thanks,

0

Good News!!

I solved the problem. Both methods, getJSON and ajax, are working, the problem was the server.

When I accessed the Web Services from a external server (not localhost) it worked.

Thanks,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.