gpt4 book ai didi

javascript - Android/iOS WebView 未遵循 CORS HTTP 重定向 (30x)

转载 作者:太空狗 更新时间:2023-10-29 13:24:30 24 4
gpt4 key购买 nike

场景:

  • WebView (Android 4.x) 通过 http(不是 file://)加载页面
  • 该页面包含对另一个域上的资源的 xhr 请求( native 或 jQuery,相同的结果)
  • 资源通过 307(或任何其他 30x,结果相同)重定向到另一个资源(某个域或其他域,结果相同)
  • 我得到臭名昭著的“错误”以及任何其他信息

注意:

  • 如果不是重定向,则请求已完成
  • 如果所有资源都在同一台服务器上,则请求已完成
  • 为重定向的资源和最终的资源都设置了 CORS header
  • 在 Chrome 或 Firefox 上一切正常
  • 我掌控一切(Android 应用程序、服务器和网页)
  • “手动”重定向有效,如果我单击 A 元素,WebView 将跟随 307
  • WebView 未命中最终 URL,这(显然)意味着它在第一次响应时检测到错误
  • 我不使用 BASE 元素

JS代码基本就是这个


var url = "http://xxxxxx/zot";
var oReq = new XMLHttpRequest();
oReq.onerror = function() {
//我收到错误
};
oReq.onload = function(e) {
//还没到
};
oReq.open("GET", url, true);
oReq.send();

第一个请求吐出的 header :

HTTP/1.1 307 Temporary Redirect
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: connection,host,accept-language,accept-encoding,accept-charset,user-agent,origin,cache-control,pragma,referer,origin,X-Requested-With,x-requested-with,accept-charset,x-wap-profile,x-getzip, Access-Control-Allow-Origin,X-HTTP-Method-Override,Content-Type,Authorization,Accept
Access-Control-Allow-Methods: GET, POST, OPTIONS
Location: http://10.90.7.10:3030/r/vast
Date: Fri, 11 Apr 2014 09:40:01 GMT
Connection: keep-alive

(尝试删除全部、部分等)

最佳答案

我发现了问题。不幸的是,WebView 倾向于在每个请求中发送自定义 header ,而这一事实将触发 CORS with preflight 类请求。但是我有一个重定向,标准明确禁止预检请求( http://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0 ,第 3 节)

If the response has an HTTP status code that is not in the 2xx range [then] Apply the network error steps.

例如,我的 XPeria T 正在发送 x-wap-profilex-getzip,还有 x-requested-with

关于javascript - Android/iOS WebView 未遵循 CORS HTTP 重定向 (30x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23010260/

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