简体   繁体   English

检查服务器上是否存在映像

[英]Check image exists on server

When I use this function for verify is image upload on server, safari debugger shows error 当我使用此功能验证是否在服务器上上传了图像时,Safari调试器显示错误

function UrlExists(url)
{
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}

[Error] Failed to load resource: the server responded with a status of 404 (Not Found)
    send ([native code], line 0)
    UrlExists (myfunction.js, line 32)
    (anonymous function) (user_dialog_list.php, line 354)
    c (jquery.js, line 4)
    fireWith (jquery.js, line 4)
    k (jquery.js, line 6)
    r (jquery.js, line 6)

What this mean, and this is normal or not? 这是什么意思,这是正常与否?

Personally I would create a new Image() , set its src to the target image, and set an onload handler to do something if it does exist, as well as an onerror handler to do something if it doesn't. 我个人将创建一个new Image() ,将其src设置为目标图像,并设置onload处理程序以执行某些操作(如果存在),以及设置onerror处理程序以执行某些操作(如果不存在)。

You will still get the 404 error in your console, but that's fine - we're handling the error because we expect errors may happen! 您仍然会在控制台中收到404错误,但这很好-我们正在处理错误,因为我们希望可能会发生错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM