0

I have been reading over a lot of questions, but cant seem to find my particular problem I have a PHP script that returns a fetch array and I encode that to JSON. I have no headers in my PHP script (I read something about that but haven't got a clue)

The PHP

if (mysql_num_rows($pquery) == 1) {
   $result = mysql_fetch_assoc($pquery);
   echo json_encode($result);

}

The jQuery

$('#c_search').submit(function(){
       data = ($(this).serialize());

  $.ajax({
      url: 'actions/get_company.php',
      type: 'POST',
      data: data,
      cache: false,
      success: function(text){
          alert (text);
          alert(text[1]); // not right!
          alert(text.company[0]);// not right!
      }

}).error(function(){
    alert('error');
})
return false;

})

The text I get back:

{"id":"167","company":"ZEN","street":"1 S Congress Ave","email":"[email protected]","state":"TX","zip":"78704","phone":"512-555-1212"}

How do I handle this properly so I can place each individual piece into its own variable.

1
  • Got it pegged! Thank you all... will check in 6 minutes when stack allows. Commented Jan 25, 2012 at 18:30

5 Answers 5

3

You can use dataType to have jQuery parse the response as JSON:

$.ajax({
    url: 'actions/get_company.php',
    dataType: 'json',

    [..]

data will then be a normal Javascript Object, so you can access the fields as normal:

 alert (data.street);
Sign up to request clarification or add additional context in comments.

Comments

2
data: data,
cache: false,
dataType: 'json', // <!-- indicate that the response type is JSON => not necessary if your PHP script correctly sets the Content-Type: application/json response header
success: function(text) {
    alert(text.id);
    alert(text.company);
    alert(text.street);
    alert(text.email);
    ...      
}

1 Comment

all of these answers pointed me in the right direction, as i went down the line this is the one the made simple sense and worked first. Thanks all.. up votes.
2

You can use JSON.parse()

$('#c_search').submit(function(){
       data = ($(this).serialize());

  $.ajax({
      url: 'actions/get_company.php',
      type: 'POST',
      data: data,
      cache: false,
      success: function(text){
          var obj = JSON.parse(text);
          alert (text);
          alert(text[1]); // not right!
          alert(text.company[0]);// not right!
      }

}).error(function(){
    alert('error');
})
return false;

})

Comments

2

Try with datatype : JSON or Use $.getJSON instead of $.ajax.

Comments

1

set the dataType to json so that the json is parsed for you like

$.ajax({
dataType:'json',
});

or you can parse the json manualy by

$.ajax({
dataType:'json',
success:function(json){
     var parsedJSON = $.parseJSON(json);
 },
});

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.