我试图通过阅读 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS 来了解 CORS(跨源资源共享) .在这里,我注意到该文件指出:
Cross-site HTTP requests initiated from within scripts have been subject to well-known restrictions, for well-understood security reasons. For example HTTP Requests made using the XMLHttpRequest object were subject to the same-origin policy.
这对我来说很有意义,因为我之前遇到过以下错误:
XMLHttpRequest cannot load
http://localhost:7000
. No 'Access-Control-Allow-Origin' header is present on the requested resource. Originhttp://localhost:9000
is therefore not allowed access.
但是,为什么
curl http://localhost:7000
正确返回位于页面上的 JSON? curl
仅仅是事实吗?命令不是来自脚本吗?此外,浏览器如何识别请求来自脚本?如果这在浏览器之间有显着差异,我很想知道 Chrome 和 Firefox 如何检测到这一点。
最佳答案
在我的想法中
XMLHttpRequest 是用于获取数据和 noad 重新加载页面的 javascript 调用服务器。首先没有页面加载没有 XMLHttpRequest。
curl是php服务器函数从服务器获取数据并返回php代码,curl是服务器代码的一部分。
关于javascript - 就 CORS 而言,通过 XMLHttpRequest、curl 和浏览器 URL 获取请求之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966526/