2010-10-27 21 views
0

大家好,发出POST到谷歌图表API与AJAX

我试图使用谷歌图表API使用Ajax做出一些排行榜网站(不要什么重新加载页面)。但我有一个问题。我必须使用POST来发出请求,但我不知道如果ajax允许这样做。例如:

var xmlhttp=new XMLHttpRequest(); 

xmlhttp.open("POST","http://chart.apis.google.com/chart?",true); 
xmlhttp.setRequestHeader("Content-type","image/png"); 
xmlhttp.send("cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World"); 

xmlhttp.onreadystatechange=function() { 

    if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
     document.getElementById("div").innerHTML=xmlhttp.responseText; 
    } 
} 

没有成功,因为我认为ajax无法处理响应的类型。任何人都可以证实?有没有其他方式使用ajax来做到这一点?

回答

0

这样它会工作:

var xmlhttp=new XMLHttpRequest(); 

xmlhttp.open("POST","http://chart.apis.google.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World",true); 
xmlhttp.send(null); 
xmlhttp.onreadystatechange = checkData; 
function checkData() { 
    if (xmlhttp.readyState == 4) { 
      alert(xmlhttp.responseText); 
    } 
} 
+2

这将无法正常工作,由于跨域检查,将禁止该请求,除非服务器设置头'访问控制允许来源允许它: *',Google Charts的情况并非如此。而在上面的代码中,无论如何你都应该检查'xmlhttp.status'代码。 – 2011-04-22 10:57:11