2012-04-20 4 views
1

제대로 작동하지 않는 jquery getJSON 함수에 결과를 전달하는 PHP 코드가 있습니다. 여기 php json이 jquery DataGrid를로드합니다.

내 코드입니다 :

$data["total"]=mysql_num_rows($s); 
    while ($r = mysql_fetch_array($s)) 
    { 

     $ris_sql["utente"]=tv($r["utente"]); 
     $ris_sql["contratto"]=tv($r["contratto"]); 
     $ris_sql["login"]=tv($r["login"]); 
     $ris_sql["piattaforma"]=tv($r["piattaforma"]); 
     $ris_sql["azione"]=tv(format_valid($r["azione"])); 
     $ris_sql["data"]=tv($r["data"]); 
     $ris_sql["note"]=tv(str_replace("<br>","",$r["note"])); 
     $riga[]=$ris_sql; 
    } 
    $data["rows"]=json_encode($riga, JSON_FORCE_OBJECT); 
    echo json_encode($data); 

내가 대신 문자열 일련의 객체와 같은 JSON 결과 내 행이 요소를 볼 불을 지르고를 사용하려고하면, 내 코드에서 무슨 일입니까 ??

답변

0

$riga JSON을 이중 인코딩합니다. 브라우저로 다시 전송하려는 $data 배열의 나머지 부분을 인코딩하기 전에 $riga 부분을 별도로 인코딩하지 마십시오. 형식이없는 당신이 기대하는 경우

// Instead of 
$data["rows"] = json_encode($riga, JSON_FORCE_OBJECT); 
// This double-encodes the contents of $data before outputting back to the browser or ajax call 
echo json_encode($data); 

// Just do: 
echo json_encode($data, JSON_FORCE_OBJECT); 

, 대신 당신은 object에 배열에서 $riga 전송할 수 있습니다 다음 JSON_FORCE_OBJECT없이 전체를 인코딩합니다.

// Cast $riga from an assoc array to an instance of stdClass 
$data['rows'] = (object)$riga; 
// And then JSON encode the whole $data 
echo json_encode($data); 
+0

답변 해 주셔서 감사합니다. Michael! – haltman

+0

@haltman 그것이 당신을 위해 작동하는 경우 답변을 받아 들일 것을 잊지 마세요. –

관련 문제