这个跨域ajax请求是如何工作的?
我正在查看这个问题及其中是 http://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/ 其中包含以下代码:
var fd = new FormData();
fd.append("image", file); // Append the file
fd.append("key", "6528448c258cff474ca9701c5bab6927");
// Get your own key: http://api.imgur.com/
// Create the XHR (Cross-Domain XHR FTW!!!)
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://api.imgur.com/2/upload.json"); // Boooom!
xhr.onload = function() {
// Big win!
// The URL of the image is:
JSON.parse(xhr.responseText).upload.links.imgur_page;
}
// Ok, I don't handle the errors. An exercice for the reader.
// And now, we send the formdata
xhr.send(fd);
这个跨域请求如何工作?我认为通常存在安全限制来阻止人们这样做。
I'm looking at this question and in it is a link to http://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/ which has the following code:
var fd = new FormData();
fd.append("image", file); // Append the file
fd.append("key", "6528448c258cff474ca9701c5bab6927");
// Get your own key: http://api.imgur.com/
// Create the XHR (Cross-Domain XHR FTW!!!)
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://api.imgur.com/2/upload.json"); // Boooom!
xhr.onload = function() {
// Big win!
// The URL of the image is:
JSON.parse(xhr.responseText).upload.links.imgur_page;
}
// Ok, I don't handle the errors. An exercice for the reader.
// And now, we send the formdata
xhr.send(fd);
How does this cross domain request work? I thought as a rule there are security restrictions that stop people from doing just this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
服务器正在响应 Access-Control-Allow-Origin 设置以允许跨域请求
http://hacks.mozilla.org/2009/07/cross-站点 xmlhttprequest-with-cors
The server is reponding with the Access-Control-Allow-Origin set to allow cross domain requests
http://www.w3.org/TR/cors/#access-control-allow-origin-response-hea
http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors
Imgur 实现了跨域资源共享(CORS)。
请参阅http://hacks.mozilla.org/2009/07 /cross-site-xmlhttprequest-with-cors/ 了解更多信息。
Imgur implements Cross-Origin Resource Sharing (CORS).
See http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ for more information.