2012-12-26 2 views
4

DB에서 반환되고 JSON 객체로 인코딩 된 정보를 구문 분석하려고합니다.JSON 객체를 Ajax를 통해 전달합니다.

$.ajax({ 
     type: "POST", 
     url: "lib/search/search.standards_one.php", 
     async: "false", 
     data: {subjects: subjects, grades: grades}, 
     success: function(response){ 
       $("#standards_results").html(""); 
       var obj = $.parseJSON(response); 
       $.each(obj, function(){ 
        alert(this['code'] + ", " + this['standard_one_id']) 
       }); 
      } 
     }); 

나는 다양한 방법을 시도했습니다 :

private function retrieve_standards_one(){ 
    $dbh = $this->connect(); 
    $stmt = $dbh->prepare("SELECT code, standard_one_id 
          FROM standard_one 
          WHERE grade_id = :grade_id 
          ORDER BY standard_one_id"); 
    $stnd = array(); 
    for($x = 0; $x < (count($this->grades)); $x++){      
    $stmt->bindParam(':grade_id', $this->grades[$x], PDO::PARAM_STR); 
    $stmt->execute(); 
    $stnd[] = $stmt->fetchAll(PDO::FETCH_ASSOC); 
    } 
    $json = json_encode($stnd); 
    return $json; 
} 

내가 정보를 분석하기 위해 노력하고있어 방법이 ID :

는 정보를 검색하는 코드입니다 이것을해라. 그러나 나는 단지 [object] [object]를 응답으로 얻는다.

http://i.imgur.com/E5Hux.png

+3

'[개체 개체]'개체의 기본 문자열 표현이 너무 코드가 아마 제대로 작동이다.'alert'는 ** 아주 나쁜 ** 디버깅 도구입니다 –

+0

consol.log는 여전히 [objects]를 반환합니다. – Brendan

+0

문자열 연결을 제거 했습니까? console.log (예 : ..이 [ '코드']);을 console.log (이 [ 'standard_one_id'])'당신은 피해야한다 무엇 인 문자열로 오브젝트를 변환합니다 모두,'alert' 및 문자열 연결은 –

답변

2

사용

console.log(this['code'] , this['standard_one_id']) 

대신

alert(this['code'] + ", " + this['standard_one_id']) 
+0

'console.log (this [ 'code'], this [ 'standard_one_id'])'가되어야합니다. 문자열 연결을 유지하면 큰 차이가 없습니다. –

+0

예, 그것은 단지 [개체]의 당신이 당신의 아약스 응답 –

+0

consol.log을 줄 수 있습니까? – Brendan

0

하여 AJAX 호출에 데이터 유형 속성을 추가

는 반응이다.

$ 아약스 ({

type: "POST", 
    dataType: "JSON", 
    url: "lib/search/search.standards_one.php", 
    async: "false", 
    data: {subjects: subjects, grades: grades}, 
    success: function(response){ 
      $("#standards_results").html(""); 
      var obj = $.parseJSON(response); 
      $.each(obj, function(){ 
       alert(this['code'] + ", " + this['standard_one_id']) 
      }); 
     } 
    }); 
+0

: 아약스에서에서 responseText를 의미 원래의 질문 –

관련 문제