2
一個演示我只想用jquery $.getJSON
函數,但現在我必須導入jquery,所以我想用原生javascript寫jquery $.getJSON
函數。
我的代碼是:
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();
}
};
使用鍍鉻顯示:
XMLHttpRequest cannot load xxxx Origin xx is not allowed by Access-Control-Allow-Origin.
誰可以幫我?
你爲什麼不讀jquery的源代碼,並使用其實施?它肯定比任何人都能在幾分鐘內想出的任何東西都更強大和更好的測試。 – 2013-04-25 12:44:28