I'm using Ajax to pass my form data and files to a PHP file for processing.
我使用Ajax将表单数据和文件传递给PHP文件进行处理。
Javascript:
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.
ValidateApplication。php肯定存在。如果我在web浏览器中输入地址,我可以查看它,但是当我提交表单时,chrome控制台返回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.
PHP与JavaScript运行的HTML页面位于同一个文件夹中,因此我很困惑为什么会一直得到404页面。
UPDATE
更新
Changing POST to GET gets rid of the 404 error, but returns a 500 Internal Server Error
更改POST以消除404错误,但返回500个内部服务器错误
UPDATE 2
更新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.
将表单的操作更改为=“ValidateApplication”。php“并按常规(不使用AJAX)提交它会导致正确的文件,不会出现任何错误。
4 个解决方案
#1
3
It seemed to be a problem with the FormData
object. Once I changed my method to use .serialize()
instead, the page worked just fine.
它似乎是FormData对象的问题。一旦我将方法改为使用.serialize(),页面就可以正常工作了。
$("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
2
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:
我也遇到过同样的问题,在查找导致404 Not Found error的原因两小时后,我发现我最近在使用PHP中的header(),并忘记删除以下代码行:
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
After deleting it, my Ajax functions became normal again.
删除之后,我的Ajax功能又恢复了正常。
#3
2
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.
对我来说,我使用了name="name"的输入字段,这使得被调用的页面返回404。奇怪的东西,希望这能帮助别人。
#4
0
Try adding a / before the filename: url: '/ValidateApplication.php',
尝试在文件名:url: '/ValidateApplication.php'中添加一个/之前,
Try changing the request type from POST to GET and see if it works.
尝试从POST中更改请求类型,以获取并查看它是否有效。
Try commenting out parts of the code:
尝试注释代码的部分:
/*cache: false,
contentType: false,
processData: false,*/
Try another browser.
尝试另一个浏览器。
#1
3
It seemed to be a problem with the FormData
object. Once I changed my method to use .serialize()
instead, the page worked just fine.
它似乎是FormData对象的问题。一旦我将方法改为使用.serialize(),页面就可以正常工作了。
$("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
2
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:
我也遇到过同样的问题,在查找导致404 Not Found error的原因两小时后,我发现我最近在使用PHP中的header(),并忘记删除以下代码行:
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
After deleting it, my Ajax functions became normal again.
删除之后,我的Ajax功能又恢复了正常。
#3
2
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.
对我来说,我使用了name="name"的输入字段,这使得被调用的页面返回404。奇怪的东西,希望这能帮助别人。
#4
0
Try adding a / before the filename: url: '/ValidateApplication.php',
尝试在文件名:url: '/ValidateApplication.php'中添加一个/之前,
Try changing the request type from POST to GET and see if it works.
尝试从POST中更改请求类型,以获取并查看它是否有效。
Try commenting out parts of the code:
尝试注释代码的部分:
/*cache: false,
contentType: false,
processData: false,*/
Try another browser.
尝试另一个浏览器。