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

将数据数组从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');
       }
     }
 });

图片转代码服务由CSDN问答提供 功能建议

为我孩子的bday建立一个网站,这将允许他的祖父母登录并点击15gig值得的照片/视频 从过去两年的每一天。 但似乎无法从jQuery中的PHP程序中捕获任何数据。 我一直收到一条错误,上面写着: “无法加载资源:net :: ERR_EMPTY_RESPONSE(03:27:54:409 |错误,网络在public_html / pics / desi / desiPics.php”。 我认为程序相对简单。我是不是以某种方式使它过于复杂?

来自desiPics.php:

 &lt;?  php 
 $ dir = glob('pics / desi / {*。jpg,* jpeg,* png,* gif}',GLOB_BRACE); 
echo json_encode($ dir); 
?&gt; 
   
 
 

来自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');
       }
     }
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题