javascript - How to send HttpRequest, and it returns Json data and display it -
i got confused on getting json data. please correct me if wrong. below code:
var xmlhttp; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { var data = json.parse(xmlhttp.responsetext); document.getelementbyid("getlistings").innerhtml=data; } } xmlhttp.open("get","https://getjsondata",true); xmlhttp.setrequestheader("content-type","application/x-www-form-urlencoded"); request.setrequestheader("accept", "application/json; charset=utf-8"); xmlhttp.setrequestheader("accept-datetime","fri, 15 feb 2013 00:00:00 gmt"); xmlhttp.setrequestheader("authorization","xxxxxxx"); xmlhttp.send();
i not displayed on "getlistings" div.
assuming have jquery:
$.ajax({ type: "get", url: "https://getjsondata", datatype: "json", success: processdata, error: function(){ alert("failed"); } }); function processdata(data) { //do data }
also, not sure you're expecting setting innerhtml json object itself, want sort of propery value form json using data.someproperty
Comments
Post a Comment