Timothy Radzikowski
Timothy Radzikowski

Reputation: 1278

jQuery ajax() returning json object but not alerting properly

How come i can't return id using data[0].id?

    $(document).ready(function(){   
$.ajax({
  type: 'POST',     
  dataType: "json",
  url: '<?php echo matry::base_to('tests/map_it');?>',
  success: function (data) 
  {
     alert(data[0])
    $('#alerts').html(data);
    data[0].id

  }

  });
});

Here's the alert that's returning.

    {"id":19385,"first":"RLY","last":"MAZI",
    "trainer_address1":"19 NE 13H CRT",
    "trainer_address2":null,"CITY":"MII","STATE":"AL",
    "trainer_zip":"33333","trainer_phone":"(721)222-4444","trainer_fax":null,
    "trainer_cell":"(213)213-  2133","website_trainer_id":115,"trainer_email":"[email protected]",
"trainer_group":"","inactive":null}

Any help would be greatly appreciated.

EDIT Here is the php that returns that json:

$mapit = sql::results("Select * from event.ACS.trainer where inactive is null or inactive=0");
foreach ($mapit as $row)
{
    $return[] = json_encode($row, JSON_FORCE_OBJECT);
}
echo json_encode($return);

I have to loop through and encode each row because, otherwise, the ajax function doesn't think there is json that is returned (and my data var is empty)

Upvotes: 2

Views: 1837

Answers (4)

crush
crush

Reputation: 17013

Your real issue is in your PHP:

$mapit = sql::results("Select * from event.ACS.trainer where inactive is null or inactive=0");
foreach ($mapit as $row)
{
    $return[] = json_encode($row, JSON_FORCE_OBJECT);
}
echo json_encode($return);

You are double json_encoding your data, which is causing the inner JSON to be treated as a string in JSON form when returned to your Javascript.

You should be doing it as follows:

$mapit = sql::results("Select * from event.ACS.trainer where inactive is null or inactive=0");
foreach ($mapit as $row)
{
    $return[] = $row;
}
echo json_encode($return);

Notice I've removed the innermost json_encode.

Now, you don't need to use the .parseJSON() call in your Javascript. This solution will be much more efficient than the accepted solution, which didn't address the real problem.

It will be more efficient because

  1. You aren't double encoding data that doesn't need to be encoded in PHP.
  2. You aren't decoding data that didn't need to be encoded in Javascript.

Thus, you eliminate 2 needless operations, and many wasted cycles (because those operations are contained within loops).

Upvotes: 6

Florin Petriuc
Florin Petriuc

Reputation: 1176

Try adding "Content-Type: application/json" to your response headers. Adding this header Google Chrome, Opera, Safari and IE will automatically convert your string to a JSON oject.

The parseJSON will only be needed on Firefox if you add this header.

If you don't wish to add that header then "JSONObject = jQuery.parseJSON(response);" is required in your javascript to convert the string to a JSON object.

Upvotes: 1

IanGSY
IanGSY

Reputation: 3714

It looks like it is just a single object being returned rather than an array so you should be able to access the id property using data.id , no need to specify an array index.

Upvotes: 1

Musa
Musa

Reputation: 97672

data[0] looks like JSON, so you'll have to parse it before you can use it as an object. e.g. $.parseJSON(data[0]).id

Upvotes: 2

Related Questions