php - Undefined value for response.message in AJAX -
i'm trying display response.message content tag id test. it's getting displayed undefined.
success:function(response){ console.log("response"+response); // works var msg = response.message; if(response.status=="success"){ console.log("response1"+msg); document.getelementbyid('test').innerhtml = msg; //undefined } else { jquery('#test').contents(msg); document.getelementbyid('test').innerhtml = msg; //undefined } }
the way handle parsing json response made javascript object (using json.parse), try code below.
success: function(response) { console.log("response" + response); response = json.parse(response); var msg = response.message; // works if (response.status == "success") { console.log("response1" + msg); // prints/works document.getelementbyid('test').innerhtml = msg; //undefined } else { jquery('#test').contents(msg); document.getelementbyid('test').innerhtml = msg; //undefined } }
alternatives
you should able set content type on php page before output using headers
header("content-type:application/json");
you can set datatype json in ajax call pretty standard
$.ajax({ type: "post", url: "", datatype: "json", success: function(msg) { //process success } ... });
Comments
Post a Comment