Klapsius Klapsius - 3 months ago 11
Javascript Question

value missing on AJAX response

I can't find what the error is in an AJAX call.

I have a PHP file output:

[{"value":"123","name":"My Name"}]


and this output is correct. And my AJAX call returns
undefined
after success:

$.ajax({
type: "POST",
url: "correct_file_location.php",
data: $(this.form).serialize(),
dataType: "json",
success: function (pk) {
alert(pk.value);
$("#label_id_name").text(pk.value);
},
error: function (){
alert("error");
}
});

Answer

Since the result is an array of objects, you need to first get the object from the array, and then access the properties of that object.

pk[0].value

should work.

Comments