Iterating jsonp response

initroot

Senior Member
Joined
Jul 30, 2011
Messages
898
Reaction score
45
Location
Cape Town
I have the following code:
$.ajax({
url: 'http://127.0.0.1:8899/list',
dataType: "jsonp",
jsonpCallback: "_testcb",
cache: false,
timeout: 5000,
success:processMarkers ,
error: function(jqXHR, textStatus, errorThrown)
{
alert('error ' + textStatus + " " + errorThrown);
}
});

This get's list from server in following format:
_testcb('[{"name":"","type":"","location":"","lat":"","lng":"","description":"","_id":"51d19900d7dad9500a000001"},{"name":"","type":"","location":"","lat":"","lng":"","description":"","_id":"51d19900d7dad9500a000002"},{"name":"","type":"","location":"","latitude":"","longitude":","description":"","_id":"51d19900d7dad9500a000003","lng":"","lat":""}]')

I have emptied the data values for the post, except for the id field.
How do I now iterate through this and read each value individually?

If anyone can maybe shed some light on the matter.
 
PHP:
_testcb: function(list){
  for (var i = 0; i < list.length; i++) {
     console.log(list[i]._id);
  }
}

something like that

you could also change this to be like so:

PHP:
$.ajax({
  url: 'http://127.0.0.1:8899/list',
  dataType: "jsonp",
  cache: false,
  timeout: 5000,
  success: function(list) {
    for (var i = 0; i < list.length; i++) {
      console.log(list[i]._id);
    }
  }
});
 
Last edited:
Thank you for the help! :)
PHP:
_testcb: function(list){
  for (var i = 0; i < list.length; i++) {
     console.log(list[i]._id);
  }
}

something like that

you could also change this to be like so:

PHP:
$.ajax({
  url: 'http://127.0.0.1:8899/list',
  dataType: "jsonp",
  cache: false,
  timeout: 5000,
  success: function(list) {
    for (var i = 0; i < list.length; i++) {
      console.log(list[i]._id);
    }
  }
});
This is done in the html document.
I tried the latter part,
instead the console.log(list._id); to window.alert(list._id);
Now It enters repetitive loop outputting undefined

This is code I have in my server for db to feed it:
function getMarkers(response, parsed_url)
{
collection.find(function(err, cursor)
{
response.writeHead(200, {'Content-Type': 'script/javascript'});
var items = [];

cursor.each(function(err, item)
{
if (item)
{
items.push(item);
}
else
{

response.end('markerscb(\'' + JSON.stringify(items) + '\')');


}
});
});
}
 
Thanks will test shortly, could it be that the object being send from dbserver side isn't array but only plain text, that's why all my iterations fail or go repetitive?
How do i test that?
Thanks again for help, first time using node.js and json.
 
Thanks will test shortly, could it be that the object being send from dbserver side isn't array but only plain text, that's why all my iterations fail or go repetitive?
How do i test that?
Thanks again for help, first time using node.js and json.

Would need to see the code sending the response.

Nm saw it now:

function getMarkers(response, parsed_url)
{
collection.find(function(err, cursor)
{
response.writeHead(200, {'Content-Type': 'application/json'});
var items = [];

cursor.each(function(err, item)
{
if (item)
{
items.push(item);
}
else
{

response.end('markerscb(\'' + JSON.stringify(items) + '\')');


}
});
});

try that. But why are you adding '\' to the list?
 
Last edited:
Thanks semaphore,
What code should i be using now in my html?
success: function(list) {
for (var i = 0; i < list.length; i++) {
console.log(list._id);
}
}
?
 
Top
Sign up to the MyBroadband newsletter
X