Not getting an array as result (calling a webservice by AJAX-JSON)
Posted
by
Pasargad
on Stack Overflow
See other posts from Stack Overflow
or by Pasargad
Published on 2013-11-05T03:51:17Z
Indexed on
2013/11/05
3:53 UTC
Read the original article
Hit count: 138
I'm trying to get the result of my web service as an array and then loop over the result to fetch all of the data; what I have done so far:
In my web service when I return the result I use
return json_encode($newFiles); and the result is as following:
"[{\"path\":\"c:\\\\my_images\\\\123.jpg\",\"ID\":\"123\",\"FName\":\"John\",\"LName\":\"Brown\",\"dept\":\"Hr\"}]"
tehn in my Web application I'm calling the rest web service by the following code in the RestService class:
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;
}
}
and then in my controller I have the following code:
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]);
}
});
});
but instead of giving me the first object it is just giving me quotation mark (the first charachter of the result) "
Why is that? I'm passing in JSON format and in AJAX I mentioned datatype as "JSON" !
Please let me know if you need more clarification! Thanks :)
© Stack Overflow or respective owner