我正在尝试从本地服务器向 YouTube 服务器发送请求,但 Chrome 控制台显示以下错误
Access-Control-Allow-Origin 不允许 代码是
http.open("POST", url, true);
http.setRequestHeader("Authorization", "AuthSub token=" + AccessToken);
http.setRequestHeader("GData-Version", 2);
http.setRequestHeader("X-GData-Key", "key=" + dev_key);
http.setRequestHeader("Content-Length", sendXML.length);
http.setRequestHeader("Content-Type", "application/atom+xml; charset=UTF-8");
http.onreadystatechange = function()
{
if(http.readyState == 4) {
alert(http.responseXML);
alert(http.responseText);
document.getElementById("response").innerHTML=http.getAllResponseHeaders();
}
}
http.send(sendXML);
这将通过实现 JSONP 回调函数来完成。但我不知道如何实现它
请指导我
最佳答案
由于浏览器的 same origin policy,您无法针对 YouTube API 使用 XMLHttpRequest 。相反,您应该使用 YouTube API 的 json-p 功能:
http://code.google.com/apis/youtube/2.0/developers_guide_json.html
编辑:自 2012 年 5 月 18 日起,YouTube API 支持 CORS:http://apiblog.youtube.com/2012/05/unlocking-javascripts-potential-with.html
关于api - Access-Control-Allow-Origin 不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9324284/