javascript - 就 CORS 而言,通过 XMLHttpRequest、curl 和浏览器 URL 获取请求之间有什么区别?

标签 javascript curl xmlhttprequest

我试图通过阅读 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. Origin http://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/

相关文章:

javascript - 在单击的 div 下方插入新的 Div

Javascript 遍历元素不起作用

PHP 在单独的线程中发送 POST 请求并忘记

javascript - XMLHttpRequest - 当将responseText返回到另一个函数时,它返回未定义

javascript - 在模块中查找函数

javascript - 错误 : Material-UI: The data grid component requires all rows to have a unique id property. 在 rows 属性中提供了没有 id 的行:

curl - 如何在curl中使用原始YAML文件创建Pod

php - curl 问题 : same request, 不同的响应

javascript - HTTP 缓存控制

node.js - Node js将pdf缓冲区转换回pdf