为什么在使用fetch fetch的情况下提出get请求会给我带来错误,同时以src的形式向url注入脚本标签,而不是src?

发布于 2025-02-08 08:21:33 字数 317 浏览 2 评论 0原文

如果我以这样的方式注入脚本:

((global, url) => {
    let s = document.createElement('script')
    s.src = url 
    global.document.body.appendChild(s)
})(window, 'https://example.com')

该请求将恰好通过,并且脚本将在对“ https://example.com”请求的响应中插入...但是,如果我尝试进行显式< strong>提取请求同一URL我会遇到CORS错误吗?为什么会发生?

If I inject a script programmatically like this:

((global, url) => {
    let s = document.createElement('script')
    s.src = url 
    global.document.body.appendChild(s)
})(window, 'https://example.com')

The request will go through just fine and the script will be inserted with the response to the request to "https://example.com" ... but if I try to make an explicit fetch request to the same URL I get a CORS error? Why is that happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2025-02-15 08:21:33

您刚刚发明了JSONP:D
描述来自 w3school

JSONP代表JSON带填充物。从另一个请求文件
由于跨域政策,域可能会引起问题。请求一个
来自另一个域的外部脚本没有这个问题。 JSONP
使用此优势,并使用脚本标签请求文件
XMLHTTPREQUEST对象。

这是 stackoverflow

You just invented JSONP :D
Description from w3school:

JSONP stands for JSON with Padding. Requesting a file from another
domain can cause problems, due to cross-domain policy. Requesting an
external script from another domain does not have this problem. JSONP
uses this advantage, and request files using the script tag instead of
the XMLHttpRequest object.

Here is explanation on stackoverflow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文