admin管理员组

文章数量:1192918

I'm using Ajax to pass my form data and 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 in the address into the web browser, however when I submit the form chrome console returns 404.

The PHP is in the same folder as the HTML page the JavaScript is running on so I am confused as to why I keep getting a 404.

UPDATE

Changing POST to GET gets rid of the 404 error, but returns a 500 Internal Server Error

UPDATE 2

Changing the action of the form to ="ValidateApplication.php" and submitting it as normal (without AJAX) leads to the correct file without any errors.

I'm using Ajax to pass my form data and 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 in the address into the web browser, however when I submit the form chrome console returns 404.

The PHP is in the same folder as the HTML page the JavaScript is running on so I am confused as to why I keep getting a 404.

UPDATE

Changing POST to GET gets rid of the 404 error, but returns a 500 Internal Server Error

UPDATE 2

Changing the action of the form to ="ValidateApplication.php" and submitting it as normal (without AJAX) leads to the correct file without any errors.

Share Improve this question edited Jun 23, 2014 at 17:16 Ashwin Sekar asked Jun 23, 2014 at 15:52 Ashwin SekarAshwin Sekar 1801 gold badge1 silver badge8 bronze badges 6
  • folder structure please – Mithun Satheesh Commented Jun 23, 2014 at 15:54
  • 7 Is your capitalization correct? Certain operating systems/web servers treat capitalization differently. (i.e., ValidateApplication.php is not the same as validateapplication.php nor is Validateapplication.php) – esqew Commented Jun 23, 2014 at 15:54
  • 1 try to use absolute path – RNK Commented Jun 23, 2014 at 15:56
  • 1 Also, just for sake of testing, what happens if you use an absolute path instead of a relative path to the PHP file? – Ed Meacham Commented Jun 23, 2014 at 15:56
  • 1 The capitalization is correct, the full URL is localhost/Scope%20Website/ValidateApplication.php and using the absolute path still gives the same error – Ashwin Sekar Commented Jun 23, 2014 at 16:41
 |  Show 1 more comment

8 Answers 8

Reset to default 4

I've had the same issue and after 2 hours looking for what was causing the 404 Not Found error I found that I was recently playing with the header() from PHP and had forgotten to delete the following line of code:

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

After deleting it, my Ajax functions became normal again.

It seemed to be a problem with the FormData object. Once I changed my method to use .serialize() instead, the page worked just 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);
        }
    });
}

For me, it was that I used an input field with name="name" which made the called page return a 404. Weird stuff, hope this helps someone.

Try adding a / before the filename: url: '/ValidateApplication.php',

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

Try commenting out parts of the code:

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

Try another browser.

Please validate you have provided name="" attribute properly in the form Form submit validate all bean attribute from name attribute of the input

Please check your PHP page name!

Do not use page-ajax.php, but instead use page_ajax.php.

Based on my case, I'd be sure to recheck your url address.

I had the same problem. Most probably you have renamed the file ValidateApplication.php or forgotten to rename.

本文标签: javascriptJquery ajax returning 404 not foundStack Overflow