从 CORS 策略阻止的原始本地主机获取访问,post 使用 ajax 不会。为什么?
Access to fetch from origin localhost blocked by CORS policy, post using ajax does not. Why?
这不是 CORS 的问题,所以不要建议使用浏览器插件或更改服务器代码的解决方法。我只是想了解为什么 ajax post 有效,而 fetch post 无效!
目前正在使用 jQuery 和 ajax 进行一些获取和 posting。使用 ajax 从 localhost 发出 post 请求工作正常,没有任何问题类似于这样的请求:
$.ajax({
type: 'POST',
url: 'https://api.example.com/endpoint',
cache: false,
data: {
moment: {
parameters: 'some params',
url: 'https://example.com',
caption: 'caption'
}
}
})
我已经决定开始使用 fetch 而不是 ajax,但是像使用相同本地主机条件的 fetch 一样执行 "looks" 操作失败并显示错误消息
Access to fetch at 'http://api.example.com/endpoint' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
提取请求看起来类似于:
fetch('https://api.example.com/endpoint', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
cache: 'no-cache',
body: JSON.stringify({
moment: {
parameters: 'some params',
url: 'https://example.com',
caption: 'caption'
}
})
})
我只想了解 jQuery 是如何解决这个问题的?我一直在查看 github 上的源代码,没有发现任何特别之处。或者它是否添加到 XMLHttpRequest 没有的提取 api 中?
编辑:
在网络选项卡中,ajax 请求如下所示:
General
Request URL: https://api.example.com/endpoint
Request Method: POST
Status Code: 200 OK
Remote Address: ip:80
Referrer Policy: no-referrer-when-downgrade
Headers
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
DNT: 1
Referer: http://localhost/index.html?target=...
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
虽然提取请求看起来像:
General
Request URL: https://api.example.com/endpoint
Referrer Policy: no-referrer-when-downgrade
Headers
Content-Type: application/json
DNT: 1
Referer: http://localhost/index.html?target=...
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
将 ajax 中的所有 headers 添加到提取请求中没有任何区别。
问题最终分为三个方面。
- Rory 评论道,jQuery 对 CORS 没有任何改变。这是我的错误,我有两个端点,一个用于接受跨域调用的开发,一个不接受,它们看起来非常相似并且行为相同,除了 CORS。
如果提供 none,- jQuery 默认设置
Content-Type
header 到 application/x-www-form-urlencoded; charset=UTF-8
。 Fetch 不会默认任何特定内容。
- jQuery 还会将自动发送的数据解析为 php 和 ruby 后端接受的格式。看起来像
这个:
moment[parameters]: value
moment[url]: value
moment[caption]: value
所以我不得不编写自己版本的 jQuery 的 $.param() 函数,因为我在 Javascript 中没有找到任何可以编码 child objects 还有。
这不是 CORS 的问题,所以不要建议使用浏览器插件或更改服务器代码的解决方法。我只是想了解为什么 ajax post 有效,而 fetch post 无效!
目前正在使用 jQuery 和 ajax 进行一些获取和 posting。使用 ajax 从 localhost 发出 post 请求工作正常,没有任何问题类似于这样的请求:
$.ajax({
type: 'POST',
url: 'https://api.example.com/endpoint',
cache: false,
data: {
moment: {
parameters: 'some params',
url: 'https://example.com',
caption: 'caption'
}
}
})
我已经决定开始使用 fetch 而不是 ajax,但是像使用相同本地主机条件的 fetch 一样执行 "looks" 操作失败并显示错误消息
Access to fetch at 'http://api.example.com/endpoint' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
提取请求看起来类似于:
fetch('https://api.example.com/endpoint', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
cache: 'no-cache',
body: JSON.stringify({
moment: {
parameters: 'some params',
url: 'https://example.com',
caption: 'caption'
}
})
})
我只想了解 jQuery 是如何解决这个问题的?我一直在查看 github 上的源代码,没有发现任何特别之处。或者它是否添加到 XMLHttpRequest 没有的提取 api 中?
编辑:
在网络选项卡中,ajax 请求如下所示:
General
Request URL: https://api.example.com/endpoint
Request Method: POST
Status Code: 200 OK
Remote Address: ip:80
Referrer Policy: no-referrer-when-downgrade
Headers
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
DNT: 1
Referer: http://localhost/index.html?target=...
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
虽然提取请求看起来像:
General
Request URL: https://api.example.com/endpoint
Referrer Policy: no-referrer-when-downgrade
Headers
Content-Type: application/json
DNT: 1
Referer: http://localhost/index.html?target=...
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
将 ajax 中的所有 headers 添加到提取请求中没有任何区别。
问题最终分为三个方面。
- Rory 评论道,jQuery 对 CORS 没有任何改变。这是我的错误,我有两个端点,一个用于接受跨域调用的开发,一个不接受,它们看起来非常相似并且行为相同,除了 CORS。 如果提供 none,
- jQuery 默认设置
Content-Type
header 到application/x-www-form-urlencoded; charset=UTF-8
。 Fetch 不会默认任何特定内容。 - jQuery 还会将自动发送的数据解析为 php 和 ruby 后端接受的格式。看起来像
这个:
moment[parameters]: value
moment[url]: value
moment[caption]: value
所以我不得不编写自己版本的 jQuery 的 $.param() 函数,因为我在 Javascript 中没有找到任何可以编码 child objects 还有。