cordova - PhoneGap ajax call fails everytime -
i developing mobile application using phonegap, , have access services project. using jquery-2.0.0.js , jquery-mobile-1.3.2.js.
$.ajax({ url: 'http://localhost:62465/api/account?email=johndoe@yahoo.com', datatype: 'json', success: function (data) { alert(data.name); }, error: function (xhr, type) { alert("failed load data"); alert(xhr + " " + type); } });
this ajax call fails everytime. in config.xml have following line: <access origin="*" />
where might going wrong!
the problem phonegap application requesting local file isn't webserver. local file delivered no http headers - means no "200 ok" header , no "404 not found" errors. so, status code assumed 0.
straight javascript xhr need ignore status , perform action on readystate == 4 (finished , ready). this:
var myrequest = new xmlhttprequest(); myrequest.open('get','localfile.html'); myrequest.onreadystatechange = function(){ if(myrequest.readystate == 4) { var result = myrequest.responsetext; } } myrequest.send();
in mootools, it's relatively straightforward task of implementing altered status test in request class - altering return code test accept 0 true. this:
request.implement({ issuccess: function(){ var status = this.status; return ((status >= 200 && status < 300) || status === 0); } });
jquery.... have things i'd jquery - i'll hold tongue because seems classy place.
to prepare jquery status == 0, need use event instead of success event, can test status code there.
$.ajax({ url: '/echo/html/', type: 'put', data: "email=a@b.com" }).always(function(data, textstatus, jqxhr){ switch(textstatus) { case 200: case 0: alert('success.'); break; case 404: alert('oops'); break; } });
ajax in cordova/phonegap - yay!
Comments
Post a Comment