Jquery ajax returns 404 not found

I use Ajax to transfer data and form files to a PHP file for processing.

Javascript

$("form#applyform").submit(function(){ var data = new FormData(); jQuery.each($('#file')[0].files, function(i, file) { data.append('file-'+i, file); }); $.ajax({ url: 'ValidateApplication.php', data: data, cache: false, contentType: false, processData: false, type: 'POST', success: function(data){ alert(data); } }); } 

ValidateApplication.php definitely exists. I can view it if I type the address in a web browser, however, when I submit the form, the chrome console returns 404.

PHP is in the same folder as the HTML page that JavaScript is running on, so I'm confused about why I keep getting 404.

UPDATE

Changing POST in GET gets rid of 404 error, but returns 500 Internal Server Error

UPDATE 2

Changing the form's action to = "ValidateApplication.php" and submitting it as usual (without AJAX) results in the correct file without any errors.

+6
source share
3 answers

There seemed to be a problem with the FormData object. As soon as I changed my method to use .serialize() instead, the page worked fine.

 $("form#applyform").submit(function(){ var data = $("form#applyform").serialize(); jQuery.each($('#file')[0].files, function(i, file) { data.append('file-'+i, file); }); $.ajax({ url: 'ValidateApplication.php', data: data, cache: false, contentType: false, processData: false, type: 'POST', success: function(data){ alert(data); } }); } 
+2
source

I had the same problem, and after 2 hours I was looking for what caused the 404 Not Found error, I found that I recently played with header() with PHP and forgot to delete the following line of code:

 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); 

After uninstallation, my Ajax functions became normal again.

+2
source

Try adding / before the file name: url: '/ValidateApplication.php',

Try changing the type of request from POST to GET and see if it works.

Try commenting out parts of the code:

 /*cache: false, contentType: false, processData: false,*/ 

Try a different browser.

0
source

Source: https://habr.com/ru/post/971243/


All Articles