douju7765 2015-04-07 16:23
浏览 232
已采纳

将数据数组从PHP返回到jQuery

Building a website for my kid's bday which will allow his grandparents to log on and click through 15gigs worth of pics/vids from every day of the last two years. Can't seem to capture any data from my PHP program in jQuery however. AND I keep receiving an error that reads: "Failed to load resource: net::ERR_EMPTY_RESPONSE (03:27:54:409 | error, network at public_html/pics/desi/desiPics.php". I thought the procedure was relatively simple. Am I overcomplicating it somehow?

From desiPics.php:

<?php
$dir= glob('pics/desi/{*.jpg,*jpeg,*png,*gif}', GLOB_BRACE);
echo json_encode($dir);
?>

From jQuery:

var desiPics = [];
  $.ajax({
   type: 'POST',
   url: 'pics/desi/desiPics.php',
   cache: false,
   async: true,
   success: function(result){
       if(result){
           desiPics = eval(result);
           alert(desiPics.length);
       }else{
           alert('error');
       }
     }
 });
  • 写回答

1条回答 默认 最新

  • donglu953744 2015-04-07 16:28
    关注

    You have to parse the json that is returned into an object to use it in the method you are.

    success: function(result){
       result = JSON.parse(result);
       if(result){
           desiPics = result;
           alert(desiPics.length);
       }else{
           alert('error');
       }
     }
    

    Edit: I added the dataType "json" which will convert result to an object.

    var desiPics = [];
      $.ajax({
       type: 'POST',
       url: 'pics/desi/desiPics.php',
       cache: false,
       async: true,
       dataType: "json",
       success: function(result){
       if(result){
           desiPics = result;
           alert(desiPics.length);
       }else{
           alert('error');
       }
     }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 静电纺丝煅烧后如何得到柔性纤维
  • ¥15 (标签-react native|关键词-镜像源)
  • ¥100 照片生成3D人脸视频
  • ¥15 伪装视频时长问题修改MP4的时长问题,
  • ¥15 JETSON NANO
  • ¥15 VS开发qt时如何在paintgl函数中用pushbutton控制切换纹理
  • ¥20 关于 openpyxl 处理excel文件地问题
  • ¥15 MS中不知道高分子的构型怎么构建模型
  • ¥60 QQOP数据,什么是op数据号,怎么提取op数据!能不能大量提取(语言-c语言)
  • ¥15 matlab代码 关于微分方程和嵌套的分段函数。