问题
使用jQuery,使用Ajax方法,通过url GET data时失败。
在macOS、安卓、Windows等等都可以正确运行,但在iOS开发时,这里怎么都过不去,跳到error。
localAjax = function(url){
//console.log(url);
return new Promise(function(resolve, reject){
$.ajax({
type: "GET",
url: url,
timeout: 2000,
success: function(data){resolve(data);},
error: function(jqXHR, text, e){
switch(e){
reject(networkError);
break;
}
reject(unknownError);
}
});
});
}
确定了url是正确的,在chrome里面可以访问,在iPhone simulator里的safari可以访问。
于是打开了safari自带的debug功能,查看错误提示。
方法:打开macos的safari-开发-模拟器-自动显示JSContext的网页检查器
运行你的工程
错误提示如下:
Refused to connect to http://…/demo/login because it appears in neither the connect-src directive nor the default-src directive of the Content Security Policy.
有了错误提示就好解决,对症下药。
解决方法
html文件
于是你发现了
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
罪魁祸首就是它,把它删掉。
运行,OK。