How do I check if file exists in jQuery or pure JavaScript?

前端 未结 18 2068
闹比i
闹比i 2020-11-22 00:56

How do I check if a file on my server exists in jQuery or pure JavaScript?

18条回答
  •  你的背包
    2020-11-22 01:34

    I use this script to check if a file exists (also it handles the cross origin issue):

    $.ajax(url, {
           method: 'GET',
           dataType: 'jsonp'
             })
       .done(function(response) { 
            // exists code 
        }).fail(function(response) { 
            // doesnt exist
        })
    

    Note that the following syntax error is thrown when the file being checked doesn't contain JSON.

    Uncaught SyntaxError: Unexpected token <

提交回复
热议问题