uni-app第三方app下webview在ios跨域请求失败
uni-app第三方app下webview在ios跨域请求失败 第三方app下webview在ios跨域请求失败,
- 第三方app提供内嵌webview的方式,给自定义业务使用,现在做了个前后端分离项目,放在该webview中,第三方app提供配置http url,url地址为前端服务地址,
- 使用uniapp完成h5开发和发布,使用nginx部署前端服务并配置反向代理,跨域请求后端接口,
- 安卓版app下,一切正常,
- ios版app下,webview能访问到前端服务的静态文件,但是js的后端请求失败,vconsole网页插件可以在network看到,后端请求status一直pending,若干秒后status变成0,
已做的尝试:
- 按照https://ask.dcloud.net.cn/article/36348#uiwebview 设置UIWebview,无效,
- nginx反向代理配置加
if ($request_method = 'OPTIONS') { add_header Access-Control-Allow-Origin $http_origin always; add_header Access-Control-Allow-Credentials true always; add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,OPTIONS' always; add_header Access-Control-Allow-Headers 'Authorization,X-Requested-With,Content-Type,Origin,Accept' always; add_header Access-Control-Max-Age 3600; add_header Content-Length 0; return 200; } add_header Access-Control-Allow-Origin $http_origin always; add_header Access-Control-Allow-Credentials true always; add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,OPTIONS' always; add_header Access-Control-Allow-Headers 'Authorization,X-Requested-With,Content-Type,Origin,Accept' always;
更多关于uni-app第三方app下webview在ios跨域请求失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html
解决了吗
解决了吗大佬
怎么解决的啊大佬们
if ($request_method = OPTIONS){
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Origin ‘*’;
add_header Access-Control-Allow-Methods ‘GET,POST,OPTIONS,DELETE,PUT’;
add_header Access-Control-Allow-Headers ‘Cookie,fssource,x-requested-with,content-type,token’;
return 204;
}
nginx反向代理按我这样配置即可解决。
这个问题主要涉及iOS WebView的安全策略限制。根据你的描述,问题很可能出在iOS WebView对跨域请求的处理比Android更严格。
关键点在于iOS的UIWebView/WKWebView默认会阻止某些跨域请求,特别是当请求包含自定义header或使用非简单请求时。你配置的nginx CORS设置是正确的,但iOS WebView可能还需要额外配置。
建议检查以下几点:
-
确认WebView类型:iOS 8+默认使用WKWebView,它的安全策略更严格。需要第三方App在创建WebView时配置允许跨域:
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.preferences.javaScriptCanOpenWindowsAutomatically = YES; config.allowsInlineMediaPlayback = YES; -
检查请求类型:iOS可能拦截了包含自定义header的请求。可以尝试:
- 简化请求header,避免不必要的自定义header
- 确保Content-Type是简单类型(application/x-www-form-urlencoded, multipart/form-data, text/plain)
-
尝试修改uni-app请求配置:
uni.request({ url: 'your-api', method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, withCredentials: true })

