Hi I have a javascript code that connects to a php script via ajax. This php script returns an array. In the success function of the ajax call, I use the returned array to display information to the user. This all works fine in all the browsers I have tried except Internet Explorer. I get the following error:
Unable to get property '0' of undefined or null reference
'0' is the index of the first element in the array. Here is the code:
JS
$.ajax({
type: "POST",
url: "/add.php",
data: 'id=' + itemid,
dataType: "json",
success: function (data) {
document.getElementById("name").innerHTML = data[0];
document.getElementById("desc").innerHTML = data[1];
document.getElementById("price").innerHTML = data[2];
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
PHP
$output = array();
$output[0] = $itemname;
$output[1] = $itemdescription;
$output[2] = $itemprice;
echo json_encode($output);
exit();
I tried console.log(data) in the success function and in Internet Explorer it returns null whereas other browsers it returns the array. Does anyone know what is wrong here?
The error code on the console in IE is SCRIPT5007. Upon searching this, this means:
You attempted to invoke the Object.prototype.toString or Object.prototype.valueOf method on an object of a type other than Object. The object of this type of invocation must be of type Object.
data = JSON.parse(data)at the beginning of the success function - ?