Retrive json Data in javascript for Phonegap Project -


i trying retrieve json data wordpress blog. used json api plugin json object.

please check code. showing error status 0. , not able retrieve data.

    var xmlhttp;  window.onload = function(){          document.addeventlistener("deviceready",init,false);     }     function init(){              xmlhttp = new xmlhttprequest();              xmlhttp.open("get","http://www.foduu.com/api/get_recent_posts/", false);             xmlhttp.send();             xmlhttp.onreadystatechange = datareturn;     }     function datareturn(){              if(xmlhttp.readystate==4 && xmlhttp.status == 200){                 var jsonresponse = xmlhttp.responsetext;                 jsonresponse  = eval("("+jsonresponse+")")                   alert("jsonresponse.count_total");             }             else             {                 alert("could not connect , status:" + xmlhttp.status);             }     } 

please me this.

thank in advance.

since data comes domain (www.foduu.com) other page coming from, call considered cross-domain call. xmlhttprequest not let such requests go through (for security purposes). there 2 ways work around limitation, , both require server opt-in. first use cors; second (which works in case, since you're issuing request) use jsonp.

i tried sending request site (using fiddler) cors headers (origin) , response not contain cors-related response, service in particular doesn't support protocol. does, however, support jsonp (if send request using fiddler http://www.foduu.com/api/get_recent_posts/?callback=cb you'll see response wrapped in cb(...) call).

what should use in page. can use jsonp in "pure" javascript, creating <script> element , adding dom, you'll easier results if use library such jquery makes easier make jsonp calls service.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -