How does this Cross Domain ajax request work?(这个跨域 ajax 请求是如何工作的?)
问题描述
我正在查看 这个问题其中有一个指向 http://hacks 的链接.mozilla.org/2011/03/the-shortest-image-uploader-ever/ 有以下代码:
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.
推荐答案
服务器正在响应 Access-Control-Allow-Origin 设置以允许跨域请求
The server is reponding with the Access-Control-Allow-Origin set to allow cross domain requests
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
这篇关于这个跨域 ajax 请求是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:这个跨域 ajax 请求是如何工作的?
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01