gpt4 book ai didi

android - Android 2.3.3 上带有授权 header 和 CORS 的 Ajax GET 请求

转载 作者:行者123 更新时间:2023-11-29 02:07:38 26 4
gpt4 key购买 nike

我正在尝试在 android 2.3.3 上发出跨域 GET 请求。我在服务器上使用 CORS 过滤器。我已经在 chrome、firefox、iphone 上测试过了,它工作正常。对于 android 2.3.3,预检请求看起来不错,但实际请求并未发送 Origin header 。我正在恢复数据,但 ajax 错误输出状态为 0。我尝试将其更改为 POST 请求并且成功了!!!

这是ajax代码:

...

$.ajax( {
beforeSend: function (xhr, settings) {
xhr.withCredentials = true;
xhr.setRequestHeader('Authorization', 'Bearer myoathtoken');
},
dataType: "json",
type: "GET",
url: getMyUrl() + '/data.json',
success: function(data) {
alert("It works");
}
});

这是获取的结果(不起作用)

OPTIONS http://test2.mydomain:9990/data.json HTTP/1.1
Host: test2.mydomain:9990
Accept-Encoding: gzip
Accept-Language: en-US
Access-Control-Request-Headers: Authorization, Accept
Cookie: mycookie=mycookieval
Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7
Referer: http://test.mydomain:9990/
User-Agent: Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; sdk Build/GRI34) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Origin: http://test.mydomain:9990
Access-Control-Request-Method: GET
Accept: text/xml, text/html, application/xhtml+xml, image/png, text/plain, */*;q=0.8

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: http://test.mydomain:9990
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: HEAD, GET, OPTIONS, POST
Access-Control-Allow-Headers: Authorization, X-Requested-With, Origin, Accept, Content-Type
Content-Length: 0
Date: Fri, 03 Feb 2012 21:29:05 GMT

GET http://test2.mydomain:9990/data.json HTTP/1.1
Host: test2.mydomain:9990
Accept-Encoding: gzip
Referer: http://test.mydomain:9990/
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; sdk Build/GRI34) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Cookie: mycookie=mycookieval
Authorization: Bearer oauthtoken
Accept: application/json, text/javascript, */*; q=0.01
Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Cache-Control: no-cache
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 03 Feb 2012 21:29:08 GMT

这是发送帖子的结果(有效)

OPTIONS http://test2.mydomain:9990/data.json HTTP/1.1
Host: test2.mydomain:9990
Accept-Encoding: gzip
Accept-Language: en-US
Access-Control-Request-Headers: Authorization, Accept
Cookie: mycookie=mycookieval
Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7
Referer: http://test.mydomain:9990/
User-Agent: Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; sdk Build/GRI34) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Origin: http://test.mydomain:9990
Access-Control-Request-Method: POST
Accept: text/xml, text/html, application/xhtml+xml, image/png, text/plain, */*;q=0.8

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: http://test.mydomain:9990
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: OPTIONS, POST, HEAD, GET
Access-Control-Allow-Headers: Authorization, X-Requested-With, Origin, Accept, Content-Type
Content-Length: 0
Date: Fri, 03 Feb 2012 21:21:30 GMT

POST http://test2.mydomain:9990/data.json HTTP/1.1
Host: test2.mydomain:9990
Accept-Encoding: gzip
Accept-Language: en-US
Cookie: mycookie=mycookieval
Authorization: Bearer oauthtoken
Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7
Referer: http://test.mydomain:9990/
User-Agent: Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; sdk Build/GRI34) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Origin: http://test.mydomain:9990
Accept: application/json, text/javascript, */*; q=0.01
Content-Length: 0

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: http://test.mydomain:9990
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 03 Feb 2012 21:21:32 GMT

我做了一些更多的测试,并从等式中删除了授权 header ,它与执行 GET 一起工作。不确定为什么授权 header 会导致获取请求失败。有什么想法吗?

最佳答案

我有同样的问题,但这不是授权 header 的问题,而是 GET 请求中缺少 Origin header 的问题。这也是 post 请求有效的原因 - 有一个 Origin Header 并且 Origin Header 对于 CORS 是强制性的。

奇怪的是 OPTIONS Preflight 调用设置了 Origin Header。

关于android - Android 2.3.3 上带有授权 header 和 CORS 的 Ajax GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9146491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com