2013-11-05 2 views
0

배열로서 웹 서비스의 결과를 얻고 결과를 반복하여 모든 데이터를 가져 오려고합니다. 내가 지금까지했던 : 나는 결과를 반환 할 때 내가 결과로 배열을 가져 오지 못했습니다 (AJAX-JSON에 의해 ​​webservice 호출)

return json_encode($newFiles); 

를 사용하여 내 웹 서비스에서

그 결과는 다음과 같습니다 :

"[{\"path\":\"c:\\\\my_images\\\\123.jpg\",\"ID\":\"123\",\"FName\":\"John\",\"LName\":\"Brown\",\"dept\":\"Hr\"}]" 

다음 내 웹 응용 프로그램에서 내가 RestService 클래스의 다음 코드로 나머지 웹 서비스를 호출하십시오.

public function getNewImages($time) { 
    $url = $this->rest_url['MyService'] . "?action=getAllNewPhotos&accessKey=" . $this->rest_key['MyService'] . "&lastcheck=" . $time; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $data = curl_exec($ch); 
    if ($data) { 
     return json_decode($data); 
    } else { 
     return null; 
    } 
} 

내 제어 도구

public function getNewImgs($time="2011-11-03 14:35:08") { 
    $newImgs = $this->restservice->getNewImages($time); 
    echo json_encode$newImgs;   
} 
and I'm calling this `enter code here`controller method by AJAX: 

$("#searchNewImgManually").click(function(e) { 
     e.preventDefault(); 
     $.ajax({ 
      type: "POST", 
      async: true, 
      datatype: "json", 
      url: "<?PHP echo base_url("myProjectController/getNewImgs"); ?>", 
      success: function(imgsResults) { 
       alert(imgsResults[0]); 
      } 

     }); 
    }); 

대신 나에게 그냥 나에게 따옴표 (결과의 첫 번째 charachter)를주고 첫 번째 개체를주는 "

왜 즉 : R 나는 다음과 같은 코드가? JSON 형식으로 전달하고 AJAX에서 데이터 유형을 "JSON"으로 언급했습니다.

더 자세한 설명이 필요하면 알려주세요. 고마워요 :)

답변

1

저는 여기서 사지에 나가고 있습니다. 그러나 $ajax(. . . 전화로 돌아 오는 중 JSON.parse(imgResults)으로 전화해야한다고 생각합니다. 나는 alert(imgResults[0])이 인용 부호 인 json 응답의 첫 번째 문자를 반환하고 있다고 생각합니다. 반환되는 json이 개체이므로 경고에 "[Object] [Object]"가 표시됩니다. json의 개별 요소에 액세스해야합니다.

returnObj = JSON.parse(imgResults); 
alert("object field is: " + returnObj.path); //or whatever relevant field you need to see to verify 
관련 문제