Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP返回字符串并通知jQuery响应_Php_Json_Ajax_Jquery File Upload - Fatal编程技术网

从PHP返回字符串并通知jQuery响应

从PHP返回字符串并通知jQuery响应,php,json,ajax,jquery-file-upload,Php,Json,Ajax,Jquery File Upload,我正在尝试做一些非常简单的事情: jQuery post->哪个将从服务器返回字符串->警报字符串 但就我的一生而言,我无法得到警报来显示字符串,我得到对象或警告 我做错了什么 结果: JSON.parse:JSON数据第1行第2列处的意外字符 例外:循环对象 [对象] 更新Php代码: public function fileupload(){ $uniqueID = request('uniqueId'); if($uniqueID != ''){ echo

我正在尝试做一些非常简单的事情:

jQuery post->哪个将从服务器返回字符串->警报字符串

但就我的一生而言,我无法得到警报来显示字符串,我得到对象或警告

我做错了什么

结果:

JSON.parse:JSON数据第1行第2列处的意外字符

例外:循环对象

[对象]

更新Php代码:

public function fileupload(){
    $uniqueID = request('uniqueId');
    if($uniqueID != ''){
         echo 'Works'
    }
    else{
         echo 'Failed'
    }
}
更新的jQuery代码:警报位于最后一块代码上

$(function () {
  
   $('.fileupload').fileupload({
      
      headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      },
      maxFileSize: 3000000,
      acceptFileTypes:  /(\.|\/)(pdf|jpeg)$/i,
      dataType: 'json',
      done: function (e, data) {
         $.each(data.result.files, function (index, file) {
            $('<p/>').text(file.name).appendTo(document.body);          
         });           
      },
         // Required for Progress bar
      progress: function (e, data) {
         var progress = parseInt(data.loaded / data.total * 100, 10);
         $(this).closest("div").find("div.bar").css(                                                   
            'width', progress + '%'            
         );
         // update text in progress bar
        $(this).closest("div").find("div.percentage").text(progress + '%');        
      }
      // This is required for displaying errors
      }).bind('fileuploadsubmit', function (e, data) {
         data.formData = {
            'uniqueId': $('.uniqueFileId').val()
         };
         
      }).on('fileuploadadd', function (e, data) {
         data.context = $('<div/>').appendTo('#files');
         $.each(data.files, function () {
         var node = $('<p/>').append($('<span/>'));     
         node.appendTo(data.context);
      });
      
      // This is also required for displaying errors 
      }).on('fileuploadprocessalways', function (e, data) {
          
         alert(data);
      
         var index = data.index,
            file = data.files[index],
            node = $(data.context.children()[index]);
            
         if (file.error) {
            node.append($('<span style=\'color:red; \'"/>').text(file.name +' '+ file.error + ',file must be .pdf or .jpg'));
          
         }
      });
   });
$(函数(){
$('.fileupload').fileupload({
标题:{
'X-CSRF-TOKEN':$('meta[name=“CSRF-TOKEN”]).attr('content'))
},
最大文件大小:3000000,
acceptFileTypes:/(\.\/)(pdf | jpeg)$/i,
数据类型:“json”,
完成:功能(e,数据){
$.each(data.result.files,函数(索引,文件){
$('

').text(file.name).appendTo(document.body); }); }, //进度条必需的 进度:功能(e、数据){ var progress=parseInt(data.loaded/data.total*100,10); $(this.closest(“div”).find(“div.bar”).css( “宽度”,进度+“%” ); //更新进度条中的文本 $(this).closest(“div”).find(“div.percentage”).text(progress+'%'); } //这是显示错误所必需的 }).bind('fileuploadsubmit',函数(e,数据){ data.formData={ “uniqueId”:$('.uniqueFileId').val() }; }).on('fileuploadadd',函数(e,数据){ data.context=$('').appendTo('#文件'); $.each(data.files,function(){ var节点=$('

')。追加($(''); appendTo(data.context); }); //这也是显示错误所必需的 }).on('fileuploadprocessalways',函数(e,数据){ 警报(数据); var指数=data.index, file=data.files[index], node=$(data.context.children()[index]); if(file.error){


node.append($('您应该尝试以下更改:

$msg = 'Hello';
echo json_encode($msg);

如果在PHP函数中使用返回值,则返回有效。在您的情况下,您在PHP和javascript中进行通信。Ajax默认行为输出您在PHP中回显/打印的内容。

根据文档,传递给
fileuploadprocessalways
处理程序的数据对象不包含来自的Ajax响应服务器的上载处理程序

相反,它包含

两个阵列:

文件-包含所应用过程的结果

原始文件-原始上传文件

也许您想为
done
回调编写一个处理程序。

问题是您没有为邮件指定密钥

php:

public function fileupload(){
    $uniqueID = request('uniqueId');
    if($uniqueID != ''){
         echo json_encode(array("msg" => 'Works'));
    }
    else{
         echo json_encode(array("msg" => 'Failed'));
    }
}
$(function () {

   $('.fileupload').fileupload({

      headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      },
      maxFileSize: 3000000,
      acceptFileTypes:  /(\.|\/)(pdf|jpeg)$/i,
      dataType: 'json',
      done: function (e, data) {
         $.each(data.result.files, function (index, file) {
            $('<p/>').text(file.name).appendTo(document.body);          
         });           
      },
         // Required for Progress bar
      progress: function (e, data) {
         var progress = parseInt(data.loaded / data.total * 100, 10);
         $(this).closest("div").find("div.bar").css(                                                   
            'width', progress + '%'            
         );
         // update text in progress bar
        $(this).closest("div").find("div.percentage").text(progress + '%');        
      }
      // This is required for displaying errors
      }).bind('fileuploadsubmit', function (e, data) {
         data.formData = {
            'uniqueId': $('.uniqueFileId').val()
         };

      }).on('fileuploadadd', function (e, data) {
         data.context = $('<div/>').appendTo('#files');
         $.each(data.files, function () {
         var node = $('<p/>').append($('<span/>'));     
         node.appendTo(data.context);
      });

      // This is also required for displaying errors 
      }).on('fileuploadprocessalways', function (e, data) {
console.log(data);



         alert(JSON.parse(data));

         var index = data.index,
            file = data.files[index],
            node = $(data.context.children()[index]);

         if (file.error) {
            node.append($('<span style=\'color:red; \'"/>').text(file.name +' '+ file.error + ',file must be .pdf or .jpg'));

         }
      });
   });
jQuery:

public function fileupload(){
    $uniqueID = request('uniqueId');
    if($uniqueID != ''){
         echo json_encode(array("msg" => 'Works'));
    }
    else{
         echo json_encode(array("msg" => 'Failed'));
    }
}
$(function () {

   $('.fileupload').fileupload({

      headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      },
      maxFileSize: 3000000,
      acceptFileTypes:  /(\.|\/)(pdf|jpeg)$/i,
      dataType: 'json',
      done: function (e, data) {
         $.each(data.result.files, function (index, file) {
            $('<p/>').text(file.name).appendTo(document.body);          
         });           
      },
         // Required for Progress bar
      progress: function (e, data) {
         var progress = parseInt(data.loaded / data.total * 100, 10);
         $(this).closest("div").find("div.bar").css(                                                   
            'width', progress + '%'            
         );
         // update text in progress bar
        $(this).closest("div").find("div.percentage").text(progress + '%');        
      }
      // This is required for displaying errors
      }).bind('fileuploadsubmit', function (e, data) {
         data.formData = {
            'uniqueId': $('.uniqueFileId').val()
         };

      }).on('fileuploadadd', function (e, data) {
         data.context = $('<div/>').appendTo('#files');
         $.each(data.files, function () {
         var node = $('<p/>').append($('<span/>'));     
         node.appendTo(data.context);
      });

      // This is also required for displaying errors 
      }).on('fileuploadprocessalways', function (e, data) {
console.log(data);



         alert(JSON.parse(data));

         var index = data.index,
            file = data.files[index],
            node = $(data.context.children()[index]);

         if (file.error) {
            node.append($('<span style=\'color:red; \'"/>').text(file.name +' '+ file.error + ',file must be .pdf or .jpg'));

         }
      });
   });
$(函数(){
$('.fileupload').fileupload({
标题:{
'X-CSRF-TOKEN':$('meta[name=“CSRF-TOKEN”]).attr('content'))
},
最大文件大小:3000000,
acceptFileTypes:/(\.\/)(pdf | jpeg)$/i,
数据类型:“json”,
完成:功能(e,数据){
$.each(data.result.files,函数(索引,文件){
$('

').text(file.name).appendTo(document.body); }); }, //进度条必需的 进度:功能(e、数据){ var progress=parseInt(data.loaded/data.total*100,10); $(this.closest(“div”).find(“div.bar”).css( “宽度”,进度+“%” ); //更新进度条中的文本 $(this).closest(“div”).find(“div.percentage”).text(progress+'%'); } //这是显示错误所必需的 }).bind('fileuploadsubmit',函数(e,数据){ data.formData={ “uniqueId”:$('.uniqueFileId').val() }; }).on('fileuploadadd',函数(e,数据){ data.context=$('').appendTo('#文件'); $.each(data.files,function(){ var节点=$('

')。追加($(''); appendTo(data.context); }); //这也是显示错误所必需的 }).on('fileuploadprocessalways',函数(e,数据){ 控制台日志(数据); 警报(JSON.parse(data)); var指数=data.index, file=data.files[index], node=$(data.context.children()[index]); if(file.error){


node.append($('为什么要以JSON的形式发送?@VTodorov请澄清您的意思?我不明白,您是指JSON_encode();如果是这样,我假设应该这样发送,如果我错了,您可以发布一个如何发送的示例吗?请检查您是否正确粘贴了Javascript代码:引号不平衡。请发布更多代码:特别是分配数据的代码。此外,请遵循正常的疑难解答步骤,简化代码,直到l它确实有效。例如,将您的php更改为类似于
echo的“Hello world”“
@dcorking update done,这更好吗?回答很好,谢谢,我认为这会帮助其他人,但不幸的是,我的问题仍然存在。您好,谢谢您的帖子,我已经根据您的建议进行了更改,但仍然得到了JSON。解析:JSON数据警告的第1行第2列出现意外字符,并且没有收到任何警告。原因是r此警告是它试图在json_编码过程中将数组转换为字符串。请检查我是否在其中添加了控制台数据变量中得到了什么?它返回:json“msg”:Works,payload“msg”:Works我正在努力访问它,不需要在jquery端解析,因为数据类型设置为json其已完成,当前