简体   繁体   中英

Check image exists on server

When I use this function for verify is image upload on server, safari debugger shows error

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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