jQuery VS Vanilla ajax crossdomain

时间:2014-04-18 15:05:44

标签: javascript jquery ajax cross-domain

我试图在纯Javascript中对Twitter URL进行跨域ajax请求,但它不起作用。但是,尝试使用与jQuery.getJSON相同的URL,它可以工作。

小提琴: http://jsfiddle.net/Ff4Xn/1/

知道我的XHR缺少什么?

var url = 'http://urls.api.twitter.com/1/urls/count.json?url=http://www.google.fr&callback=?';

var request = new XMLHttpRequest();
request.open('GET', 'http://urls.api.twitter.com/1/urls/count.json?url=http://www.google.fr', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400){
    resp = request.responseText;
    document.getElementById('test').innerHTML = resp;
  }
};

request.onerror = function() {
    document.getElementById('vanilla').innerHTML = 'error';
};

request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
console.log('vanilla ajax');
request.send();

// jquery follows
$.getJSON(url, function(data){
    console.log('jquery ajax');
    document.getElementById('jquery').innerHTML =     data.count;
});

0 个答案:

没有答案
相关问题