2014-11-02 4 views
0
내가 PHP 파일에서 아약스를 통해 일부 데이터를 얻으려면

를 통해 데이터를 얻는 방법 : 이 내 코드 :아약스

jQuery("#load").click(function(){ 
    $.ajax({ 
     type: 'POST', 
     url: 'setting/php/get_pic.php' 
    }).done(function (data) { 
     // Bei Erfolg 
     console.log("done:" + data); 
    }); 
}); 

PHP 코드 :

$images = glob("BILDER/{*.jpg,*.JPG}", GLOB_BRACE); 
print_r($images); 

는 이제 배열을 쓰고 싶어 비트 I 추가 AJAX에서 전체 PHP 스크립트 코드

+0

작동하지 않습니다? –

답변

0

을 얻을 dataType: 'json'

그리고 PHP 최종 사용 die() 또는 echo 대신 print_r()

$images = glob("BILDER/{*.jpg,*.JPG}", GLOB_BRACE); 
die(json_encode($images)); //or echo json_encode($images); 
+0

그것은 작동하지 않습니다, 내 자신의 대답을 봐주십시오 – andy

+0

어떤 오류있어? – MH2K9

+0

난 지금은 오류가없는, 그것을 통과하지만, 결과 데이터는 – andy

0

시도

jQuery("#load").click(function(){ 
    $.ajax(
     { 
      type: 'POST', 
      url: 'setting/php/get_pic.php', 
      dataType: 'json' //Add this line 
     }) 
     .done(function (data) { 
      console.log("done:" + data); 
     }); 
}); 
. (. 예)

<!DOCTYPE html> 
    <html lang="en"> 
    <head> 
    <title>Ajax Example</title> 
    <meta charset="utf-8"> 
    <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
    <script> 
    jQuery(document).ready(function(){ 

     $.ajax(
      { 
      type: 'POST', 
      url: 'setting/php/get_pic.php' 
      }).done(function (data) { 
       // Bei Erfolg 
       jsonObj = JSON.parse(data); 

       var img = ""; 

       for(i in jsonObj) 
       { 
        img += jsonObj[i]+"<br/>"; 
       } 

       $("#jsonParsedResult").html(img); 

      }); 



}); 
    </script> 
    </head> 

    <body> 
    <div id="jsonParsedResult"></div> 
    </body> 
    </html> 

PHP 코드 : 무엇을

<?php 
$images = glob("BILDER/{*.php,*.PHP}", GLOB_BRACE); 

echo json_encode($images); 
?> 
+0

나는 이것을 시도하고, 콘솔에서 도 완료되었다 [] – andy

+0

경고 (typeof (data))를 시도하면 문자열을 얻는다. 그러나 PHP 파일에는 배열 – andy

+0

andy