2013-02-25 6 views
1

: 인 test.html에JQuery와 액세스 PHP jason_encoded 배열

<?php 
array_push($stack, "BLABLA"); 
array_push($stack, "BLABLA2"); 
echo json_encode($stack); 
?> 

:

<script type="text/javascript"> 
$(document).ready(function(){ 
    $.post( 
    'test.php', // location of your php script 
    {}, // any data you want to send to the script 
    function(data){ // a function to deal with the returned information 
     $('#mydiv').html(data[0]).show(); 
    }, "json"); 
}); 
</script> 

<div id="mydiv"></div> 

제가 몇몇 검사를 완료하고 그 데이터 [0]이 아니라고 보인다 내가 PHP에서 밀어 첫 번째 요소에 액세스하는 올바른 방법. 그러면 어떻게해야합니까?

+1

배열이'$ stack'이라고 알고 계셨습니까?'$ array'라는 변수에'json_encode()'를 사용하고 계십니까? – JakeParis

+0

당신은 서버에서받는 json을 게시 할 수 있습니까 – dakait

+0

솔직히 그것을 찾아내는 방법을 모른다! 아주 미안해. – Kam

답변

0

<?php 
    $stack = array(); 
    array_push($stack, "BLABLA"); 
    array_push($stack, "BLABLA2"); 
    echo json_encode($stack); 
?> 

을 시도하고 반드시 ID mydiv와 DIV 안에 당신에게 BLABLA을 줄 것이다 참조하십시오.

먼저 $stack을 빈 배열로 정의하거나 $stack 배열이 이미 존재하는지 확인하십시오.

firebug Firefox 부가 기능.

0

배열로 데이터를 다시 변환 :

function(data){ // a function to deal with the returned information 
    data = JSON.parse(data); // convert the JSON data back to an array 
    $('#mydiv').html(data[0]).show(); 
}, "json");