0

I am trying to check if a file exist or not but when I run the code it always go to error callback function even if the filename is the url of an existing file. Why does $.ajax enter only the error callback function?

$.ajax({
       url: filename,
       type: 'GET',
       dataType: 'text',
       async: false,
       cache: false,
       success: function ()
       {
            alert('file is found.');
       },
       error: function ()
       {
            alert('file is not found.');
       }
 });
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • Is your file name is link(remote file)?If so file_exists function always return false.Type here your file name – Shaiful Islam Nov 19 '14 at 15:11
  • Instead of type: 'GET', this SO answer suggests using 'HEAD' - there are other useful answers as well - possible duplicate: http://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-javascript – ron tornambe Nov 19 '14 at 15:22

3 Answers3

0

There could be more problems:

  1. The file might not be where you think it is

  2. The file might crash and fail to respond

  3. The file might run for a long while and your request might crash due to timeout

You should check all these possibilities.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

You have to do a script jquery with a file php.

$.ajax({ url: verify.php, 
        data: {file: filename}
        type: 'GET', dataType: 'text', async: false, cache: false, 
        success: function (data) { if(data){alert('file is found.');} }, 
        error: function () { alert('file is not found.'); } 
 });

And the code php

<?php
     echo file_exists($_GET['file']);
?>

Or you can check the permissions of the file

0

I think your return string data is very long. so the JSON format has been corrupted. You should change the max size for JSON data in this way :

Open the Web.Config file and paste these lines into the configuration section.

<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="50000000"/>
    </webServices>
  </scripting>
</system.web.extensions>
Emad Armoun
  • 1,625
  • 2
  • 19
  • 32