ajax - How to use native javascript write jquery $.getJSON function? -
a demo want use jquery $.getjson
function,but must import jquery, want use native javascript write jquery $.getjson
function.
my code is:
var $={ getjson: function(url, params, callback){ var requrl = url; var xhr = new xmlhttprequest; xhr.onreadystatechange = function() { if (xhr.readystate == 4 && xhr.status == 200) { json.parse(xhr.responsetext); } } xhr.open("get", requrl); xhr.send(); } };
use chrome show:
xmlhttprequest cannot load xxxx origin xx not allowed access-control-allow-origin.
who can me?
make ajax request , use json.parse
on result. like:
xhr = new xmlhttprequest; xhr.onreadystatechange = function() { if (xhr.readystate == 4 && xhr.status == 200) { json.parse(xhr.responsetext); } } xhr.open("get", url) xhr.send();