Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using a jquery ajax call which accepts json response :

 var posturl = '/admin/getparamdetails/';
    var data = "adnetworkId="+adnetworkId;
    $.ajax({
        type: "POST",
        url: posturl,
        data : data,
        datatype: "json",
        success: function(msg){
            //$("#displayPramForm").html(msg);
            //alert('hello'+msg.length+' '+msg.hello.length);
            console.log(msg);
            if(msg!='')
            {
                alert(msg.hello);
            }
        },
        failure: function(msg){}
    });

in my php backend function , I am using json_encode on a simple array as shown:

 $json_encoded_string =  json_encode(array("hello"=>'abc'));
 echo $json_encoded_string;
 die;               

but alert(msg.hello) returns undefined for me. What is going wrong here ? Also , in my console.log I am able to get the output as :

{"hello":"abc"}     
share|improve this question

2 Answers

up vote 2 down vote accepted

Use parseJSON on the return data:

if (msg) {
  msg = $.parseJSON(msg);
  alert(msg.hello);
}
share|improve this answer
thanks @powers1...it did work.. – debaShish Dec 15 '11 at 10:50

you have to send the data as Content-Type "application/json", otherwise it won't work.

Just add the following in your PHP File:

header('Content-type: application/json');
share|improve this answer
nope ,still it didn't work. – debaShish Dec 15 '11 at 10:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.