7

I am using: echo json_encode($Response); to send an Associative array back to JQuery Ajax. Whenever I try to read each ID key value I get an undefined value. Please help me figure out what I am doing so wrong... Thanks in advance

My PHP Code:

$Stuff = 'Hello world';

$Success = true;
$Content = $Stuff;

$Response = array('Success' => $Success, 'Content' => $Content);
echo json_encode($Response);
# #

My JS code:

var sFirstName     = $('#student_first_name').attr('value');  

$.ajax({  
    type: "GET",  
    url: "../pgs/UpdateEditAStudent.php", 
    data: "FirstName="+ sFirstName ,  

    //The below code will give me: {"Success":true,"Content":"Hello world"}
    success: function(data){$("#Ajax_response").html(data);}

    //The popup window will show me "Undefined"
    //and: {"Success":true,"Content":"Hello world"}
    success: function(data){$("#Ajax_response").html(data); alert(data.Content);}
});  
kmario23
  • 57,311
  • 13
  • 161
  • 150
SirBT
  • 1,580
  • 5
  • 22
  • 51

5 Answers5

10

You should set the mime type aswell, wich, according to this question is application/json. Then jQuery will understand the answer is a json element. To do it, you'd do the following:

header('Content-Type: application/json');

In your UpdateEditAStudent.php before printing anything.

Community
  • 1
  • 1
Lumbendil
  • 2,906
  • 1
  • 19
  • 24
  • @Lumbenil. Thank you very much. What is the advantage of your solution compared to defining the correct datatype in $.ajax dataType: "json", as Niklaas suggested? – SirBT Jul 06 '11 at 16:33
  • Simply correctness of the `UpdateEditAStudent.php`, since now Apache (or whatever the webserver is) is telling to anyone downloading that the given URL is an HTML page (mime type `text/html`) instead of it's true mime type, that is `application/json`. – Lumbendil Jul 07 '11 at 10:28
7

You don't have to add a header to the PHP file, just use this Jquery parseJSON function:

Keep this PHP code as it is:

$Stuff = 'Hello world';

$Success = true;
$Content = $Stuff;

$Response = array('Success' => $Success, 'Content' => $Content);
echo json_encode($Response);

And for the JS:

$.ajax({  
    type: "GET",
    url: "../pgs/UpdateEditAStudent.php",
    data: "FirstName="+ $('#student_first_name').val(),

    success: function(data){
        // Here is the tip
        var data = $.parseJSON(data);

        alert(data.Content);
    }
});
Anthony Simmon
  • 1,579
  • 12
  • 26
4

You need to define the correct dataType or provide the correct header, as Lumbendil described.

You can manually define the dataType to json, so your code would look like:

$.ajax({  
   type: "GET",  
    url: "../pgs/UpdateEditAStudent.php", 
   data: "FirstName="+ sFirstName ,  
   dataType: "json",
   ...etc
Niklas
  • 29,752
  • 5
  • 50
  • 71
1

It's an array. You should probably do alert(data['Content']);.

Stefan H Singer
  • 5,469
  • 2
  • 25
  • 26
0

do something like this

$Stuff = 'Hello world';

$Success = true;
$Content = $Stuff;

$Response = array('Success' => $Success, 'Content' => $Content);
echo json_encode($Response);
Michael
  • 239
  • 3
  • 12
asdasd
  • 11