小编典典

此跨域ajax请求如何工作?

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);

此跨域请求如何工作?我认为通常有一些安全限制阻止人们这样做。


阅读 240

收藏
2020-07-26

共1个答案

小编典典

服务器正在响应,并将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

2020-07-26