webview的CORS跨域
by addy 原创文章,欢迎转载,但希望全文转载,注明本文地址。
本文地址:http://www.iamaddy.net/2014/04/webview%e7%9a%84cors%e8%b7%a8%e5%9f%9f/
在项目中遇到个问题,页面嵌入在webview中。正常浏览器的如UC浏览器、QQ浏览器中使用CORS跨域请求是没问题,但在webview中却拉取失败,照理都是同样的请求,应该不是后台的问题。catch到的错误是
INVALID_STATE_ERR: DOM Exception 11
然后google之,发现是zepto的一个bug,git上已经说明,应该下个版本会合并上。
解决方案暂且依照git上所说:
由于跨域需要发送cookie,因此需要加上withCredentials = true
这句代码
官方说明:
http://www.w3.org/TR/XMLHttpRequest2/#the-withcredentials-attribute
Therefore, use the open method on the XMLHttpRequest object before setting the withCredentials attribute
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("POST", "url", true);
xhr.send();
这样就会报错
var xhr = new XMLHttpRequest();
xhr.open("POST", "url", true);
xhr.withCredentials = true;
xhr.send();
改成这样就好了
具体参考:
https://github.com/madrobby/zepto/issues/921
本文为原创文章,可能会经常更新知识点以及修正一些错误,因此转载请保留原出处,方便溯源,谢谢合作
本文地址:http://www.iamaddy.net/2014/04/webview%e7%9a%84cors%e8%b7%a8%e5%9f%9f/
个人知乎,欢迎关注:https://www.zhihu.com/people/iamaddy
欢迎关注公众号【入门游戏开发】
近期评论