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 如何输入百度,显示本地下载的html文件页面,地址栏还显示百度的地址
  • ¥15 通过kinect制作换装程序但是服装不贴合(标签-ar)
  • ¥20 matlab如何绘制三维瀑布图
  • ¥15 关于用abap来解决动态规划的问题,但是要求输出索引值,这个是难点
  • ¥15 在ISIS中什么是IP从地址
  • ¥15 压测时,并发量过高时,响应时间出现尖刺
  • ¥15 关于vmprotect3.8.4虚拟文件一项
  • ¥15 在不用IT调试的情况下怎样能连外网
  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面