Jquery ajax返回404未找到 [英] Jquery ajax returning 404 not found

查看:65
本文介绍了Jquery ajax返回404未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ajax将表单数据和文件传递给PHP文件进行处理。

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 肯定存在。如果我在Web浏览器中输入地址,我可以查看它,但是当我提交表单时,Chrome控制台返回404。

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.

PHP与运行JavaScript的HTML页面位于同一个文件夹中,所以我很困惑为什么我会继续获得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

将POST更改为GET可以消除404错误,但会返回500内部服务器错误

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

更新2

将表单的操作更改为=ValidateApplication.php并正常提交(没有AJAX)会导致正确的文件没有任何错误。

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

推荐答案

FormData 对象似乎有问题。一旦我改变了我的方法,使用 .serialize(),页面工作得很好。

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);
        }
    });
}

这篇关于Jquery ajax返回404未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆