Javascript phonegap+;iOS、http请求始终在本地文件上返回状态0
我正在使用Phonegap 3.3.0,在iOS上,无论文件是否存在,以下http请求始终返回0Javascript phonegap+;iOS、http请求始终在本地文件上返回状态0,javascript,ios,http,cordova,Javascript,Ios,Http,Cordova,我正在使用Phonegap 3.3.0,在iOS上,无论文件是否存在,以下http请求始终返回0 var url = './images/pros/imagefile.png'; var http = new XMLHttpRequest(); http.open('HEAD', url, false); http.send(); http.status 如果文件存在,则返回0,但如果我尝试了错误的url: var url = 'httpbabla/images/pros/image
var url = './images/pros/imagefile.png';
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
http.status
如果文件存在,则返回0,但如果我尝试了错误的url:
var url = 'httpbabla/images/pros/imagefile.pngfkjdqmkfjdmqsl';
或
它仍然返回0
这是电话漏洞吗?如果没有,那么如何使用Phonegap和iOS快速区分现有本地文件和不存在的文件
谢谢对于异步请求和
onload
和onerror
处理程序,您可能会有更好的运气。注意使用true
参数作为http.open
的最后一个参数:
var url = 'file:///does/not/exist.jpg';
var http = new XMLHttpRequest();
http.open('HEAD', url, true);
http.onload = function(e){/* Success! File exists; process the response... */};
http.onerror = function(e){/* Failed - file does not exist - do whatever needs to be done... */};
http.send();
一旦调用http.sendhttp.send,您的函数将完成,控制权将返回给调用方。在onload
和onerror
回调中,需要处理成功或失败时需要执行的任何处理。如果您不熟悉Javascript回调(和闭包),请花点时间学习如何使用它们——它们是该语言中最强大的功能之一。这对我很有用:
function UrlExists(url)
{
if ( isPhoneGap && isIOS() ) {
if (url.fileExists()){ //see prototype below
return true;
}else {
return false;
}
} else {
try {
http = new XMLHttpRequest();
http.open("HEAD", url, false);
http.send();
}
catch (err) {
//alert("An error:" + err.name + "---" + err.message);
}
//return http.status!=404;
if (http.readyState == 4) {
console.log("http.status="+http.status);
return http.status==200 || http.status==0;
}
}
}
// check local file existance for iOS
String.prototype.fileExists = function() {
filename = this.trim();
var response = jQuery.ajax({
url: filename,
type: 'HEAD',
async: false
}).status;
return (response != "200") ? false : true;
}
你好@sherb,谢谢,你能帮我把它做成一个函数吗?如果有错误,我可以返回false:函数UrlExists(url){var http=new XMLHttpRequest();http.open('HEAD',url,true);http.onerror=function(e){console.log('onerror:'+JSON.stringify(e));};http.send();//返回http.status!=404;if(!isIOS()){return http.status==200;}否则{return(http.status==200 | | | http.status==0)}}
@Louis,我更新了我的答案。请注意,onerror
和onload
不是您调用的东西-它们是从XMLHttpRequest中的代码调用的回调。把你的代码放在那些函数里面。嗨,我也有同样的问题,不管本地url是否存在,响应都是一样的。你还有别的想法吗?为什么Phonegap会有这种奇怪的行为?@Louis我已经用iOS模拟器和Safari测试了我的答案。如果我不了解你的项目,我想不出还有什么建议。非常感谢,我真的不知道这是怎么回事。我找到了一个解决方法,我现在发布它。
function UrlExists(url)
{
if ( isPhoneGap && isIOS() ) {
if (url.fileExists()){ //see prototype below
return true;
}else {
return false;
}
} else {
try {
http = new XMLHttpRequest();
http.open("HEAD", url, false);
http.send();
}
catch (err) {
//alert("An error:" + err.name + "---" + err.message);
}
//return http.status!=404;
if (http.readyState == 4) {
console.log("http.status="+http.status);
return http.status==200 || http.status==0;
}
}
}
// check local file existance for iOS
String.prototype.fileExists = function() {
filename = this.trim();
var response = jQuery.ajax({
url: filename,
type: 'HEAD',
async: false
}).status;
return (response != "200") ? false : true;
}