在项目中遇到个问题,页面嵌入在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
发表回复