[英]How does this Cross Domain ajax request work?
我正在查看这个问题,它是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);
这个跨域请求如何工作? 我认为通常有安全限制阻止人们做这件事。
服务器正在响应,设置了Access-Control-Allow-Origin以允许跨域请求
Response Headers
Access-Control-Allow-Origin: *
Cache-Control max-age=604800
Connection keep-alive
Content-Length 494
Content-Type application/json
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)。
CORS标准的工作原理是添加新的HTTP标头,允许服务器将资源提供给允许的源域。 浏览器支持这些标头并强制执行它们建立的限制。 此外,对于可能对用户数据产生副作用的HTTP请求方法(特别是对于GET以外的HTTP方法,或对某些MIME类型的POST使用),规范要求浏览器“预检”请求,请求支持的方法从具有HTTP OPTIONS请求标头的服务器,然后,在服务器“批准”时,使用实际的HTTP请求方法发送实际请求。 服务器还可以通知客户端是否应随请求一起发送“凭据”(包括Cookie和HTTP身份验证数据)。
有关更多信息,请参见http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.