Ajax 成功函数不输出 Php 脚本发送的文本
Ajax success function not outputting text sent by Php script
我正在使用下面的 PHP 脚本上传多个文件:
<?php
if(isset($_FILES['uploadfile'])){
$total_files = count($_FILES['uploadfile']['name']);
if( $total_files > 0){
for($i=0; $i<$total_files; $i++) {
$file_name = $_FILES['uploadfile']['name'][$i];
$file_size = $_FILES['uploadfile']['size'][$i];
$file_tmp = $_FILES['uploadfile']['tmp_name'][$i];
$file_type = $_FILES['uploadfile']['type'][$i];
$upload_Path = "storage/".$file_name;
//var_dump($file_size);
//die;
if($file_size > 8000000){
echo ('Total upload size must be less than 8 MB.');
die;
}
if($file_tmp == ""){
echo ('There is no file path.');
die;
}
else{
if(!file_exists($upload_Path)){
move_uploaded_file($file_tmp, $upload_Path);
}
else{
$name = pathinfo($file_name, PATHINFO_FILENAME);
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_name = $name.rand().'.'.$ext;
$new_Path = "storage/".$new_name;
move_uploaded_file($file_tmp, $new_Path);
}
}
}
}
die('File uploaded successfully!');
}
?>
但问题是每当发生错误时,例如 echo ('Total upload size must be less than 8 MB.');
,它不会使用 ajax 输出。但是当上传成功后,它会显示 File uploaded successfully!
.
我的AJAX如下:
$.ajax({
type:'POST',
url: 'mupld.php',
data: formdata,
processData:false,
contentType:false,
success: function(response){
alert('Success: '+response);
},
error: function(xhr, status, error){
alert('Error: '+status+' '+error);
}
});
在进行 var 转储时,我没有得到 8mb 以上上传的任何输出,但低于 8mb 我得到
Success: <pre class='xdebug-var-dump' dir='ltr'><small>int</small> <font color='#4e9a06'>3283515</font>
</pre>
@Jeff Bucket 是对的,所以我编辑了我的答案:
实际上,您应该在成功回调中处理这些错误。 error() 回调是为浏览器和服务器之间的连接刚刚中断的情况保留的,并且 error() 参数期望处理这种情况,例如典型的 textStatus 错误应该是 'Not Found' 或 'Internal Server Error',但没有 'Total upload size must be less than 8 MB.'。
您应该 return 一个包含您可以在客户端中使用的信息的数组,并在 success() 中处理它,例如:
尝试{
if(isset($_FILES['uploadfile'])){
$total_files = count($_FILES['uploadfile']['name']);
if( $total_files > 0){
for($i=0; $i<$total_files; $i++) {
$file_name = $_FILES['uploadfile']['name'][$i];
$file_size = $_FILES['uploadfile']['size'][$i];
$file_tmp = $_FILES['uploadfile']['tmp_name'][$i];
$file_type = $_FILES['uploadfile']['type'][$i];
$upload_Path = "storage/".$file_name;
//var_dump($file_size);
//die;
if($file_size > 8000000){
echo json_encode( array('status' => 'failure' , 'msg' => 'Total upload size must be less than 8 MB.') );
die();
}
if($file_tmp == ""){
echo json_encode( array('status' => 'failure' , 'msg' => 'There is no filepath.') );
die;
}
else{
if(!file_exists($upload_Path)){
move_uploaded_file($file_tmp, $upload_Path);
}
else{
$name = pathinfo($file_name, PATHINFO_FILENAME);
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_name = $name.rand().'.'.$ext;
$new_Path = "storage/".$new_name;
move_uploaded_file($file_tmp, $new_Path);
}
}
}
}
echo json_encode( array('status' => 'success' , 'msg' => 'File uploaded succesfully.') );
die();
}
else{
echo json_encode(array("status" => "error" , "msg" => "No file was found when processing uploaded files" ) );
die();
}
}
catch(Exception $ex){
echo json_encode(array('status' => 'error' , 'msg' => 'An unhandled exception raised: ' . $ex->getMessage() ) );
die();
}
finally{
die();
}
然后在你的 $.ajax() 函数中:
$("#uploadfile").change(function(){
//submit the form here
var files = $("#fileupload")[0];
var formdata = new FormData(files);
$.ajax({
type:'POST',
url: 'mupld.php',
data: formdata,
processData:false,
contentType:false,
success: function(response){
response = JSON.parse(response);
alert(response.msg);
},
error: function(xhr, textStatus, error){
console.log('Error: '+textStatus+' '+error);
}
});
如果您特别想在 error() 回调中处理此问题,您应该使用 header() 将 php 脚本的响应代码设置为 500 - 或任何自定义代码。
我正在使用下面的 PHP 脚本上传多个文件:
<?php
if(isset($_FILES['uploadfile'])){
$total_files = count($_FILES['uploadfile']['name']);
if( $total_files > 0){
for($i=0; $i<$total_files; $i++) {
$file_name = $_FILES['uploadfile']['name'][$i];
$file_size = $_FILES['uploadfile']['size'][$i];
$file_tmp = $_FILES['uploadfile']['tmp_name'][$i];
$file_type = $_FILES['uploadfile']['type'][$i];
$upload_Path = "storage/".$file_name;
//var_dump($file_size);
//die;
if($file_size > 8000000){
echo ('Total upload size must be less than 8 MB.');
die;
}
if($file_tmp == ""){
echo ('There is no file path.');
die;
}
else{
if(!file_exists($upload_Path)){
move_uploaded_file($file_tmp, $upload_Path);
}
else{
$name = pathinfo($file_name, PATHINFO_FILENAME);
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_name = $name.rand().'.'.$ext;
$new_Path = "storage/".$new_name;
move_uploaded_file($file_tmp, $new_Path);
}
}
}
}
die('File uploaded successfully!');
}
?>
但问题是每当发生错误时,例如 echo ('Total upload size must be less than 8 MB.');
,它不会使用 ajax 输出。但是当上传成功后,它会显示 File uploaded successfully!
.
我的AJAX如下:
$.ajax({
type:'POST',
url: 'mupld.php',
data: formdata,
processData:false,
contentType:false,
success: function(response){
alert('Success: '+response);
},
error: function(xhr, status, error){
alert('Error: '+status+' '+error);
}
});
在进行 var 转储时,我没有得到 8mb 以上上传的任何输出,但低于 8mb 我得到
Success: <pre class='xdebug-var-dump' dir='ltr'><small>int</small> <font color='#4e9a06'>3283515</font>
</pre>
@Jeff Bucket 是对的,所以我编辑了我的答案:
实际上,您应该在成功回调中处理这些错误。 error() 回调是为浏览器和服务器之间的连接刚刚中断的情况保留的,并且 error() 参数期望处理这种情况,例如典型的 textStatus 错误应该是 'Not Found' 或 'Internal Server Error',但没有 'Total upload size must be less than 8 MB.'。
您应该 return 一个包含您可以在客户端中使用的信息的数组,并在 success() 中处理它,例如:
尝试{
if(isset($_FILES['uploadfile'])){
$total_files = count($_FILES['uploadfile']['name']);
if( $total_files > 0){
for($i=0; $i<$total_files; $i++) {
$file_name = $_FILES['uploadfile']['name'][$i];
$file_size = $_FILES['uploadfile']['size'][$i];
$file_tmp = $_FILES['uploadfile']['tmp_name'][$i];
$file_type = $_FILES['uploadfile']['type'][$i];
$upload_Path = "storage/".$file_name;
//var_dump($file_size);
//die;
if($file_size > 8000000){
echo json_encode( array('status' => 'failure' , 'msg' => 'Total upload size must be less than 8 MB.') );
die();
}
if($file_tmp == ""){
echo json_encode( array('status' => 'failure' , 'msg' => 'There is no filepath.') );
die;
}
else{
if(!file_exists($upload_Path)){
move_uploaded_file($file_tmp, $upload_Path);
}
else{
$name = pathinfo($file_name, PATHINFO_FILENAME);
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_name = $name.rand().'.'.$ext;
$new_Path = "storage/".$new_name;
move_uploaded_file($file_tmp, $new_Path);
}
}
}
}
echo json_encode( array('status' => 'success' , 'msg' => 'File uploaded succesfully.') );
die();
}
else{
echo json_encode(array("status" => "error" , "msg" => "No file was found when processing uploaded files" ) );
die();
}
}
catch(Exception $ex){
echo json_encode(array('status' => 'error' , 'msg' => 'An unhandled exception raised: ' . $ex->getMessage() ) );
die();
}
finally{
die();
}
然后在你的 $.ajax() 函数中:
$("#uploadfile").change(function(){
//submit the form here
var files = $("#fileupload")[0];
var formdata = new FormData(files);
$.ajax({
type:'POST',
url: 'mupld.php',
data: formdata,
processData:false,
contentType:false,
success: function(response){
response = JSON.parse(response);
alert(response.msg);
},
error: function(xhr, textStatus, error){
console.log('Error: '+textStatus+' '+error);
}
});
如果您特别想在 error() 回调中处理此问题,您应该使用 header() 将 php 脚本的响应代码设置为 500 - 或任何自定义代码。