0

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?

EDIT: 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.

Link: http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-GB&k=k(VS.WebClient.Help.SCRIPT5007)

1
  • Take a look at the output generated codepad.org/7quG3IKs Commented Mar 13, 2014 at 22:12

2 Answers 2

1

try :

$output = array();
$output[] = $itemname;
$output[] = $itemdescription;
$output[] = $itemprice;
echo json_encode($output);
exit();

Note that $output[]=xxx means : append xxx to $output where $output[1]=xxx means, put xxx at index 1 of $output.

Note that you may as well do :

$output = array($itemname, $itemdescription, $itemprice);

EDIT for comments and multiples OP edits :

Try printing the data to see what's going on :

success: function (data) {
    console.log(data);
    document.getElementById("name").innerHTML = data[0];
    document.getElementById("desc").innerHTML = data[1];
    document.getElementById("price").innerHTML = data[2];
},

You'll see what data contains, for IE, in the console (use F12)

6
  • Sorry there was a mistake look at the block of code again in my question. I am adding to 3 different indexes Commented Mar 13, 2014 at 22:20
  • you need to set index 0, which you use in javascript. So then you'll set $output[0] = $itemname; $output[1] = $itemdescription; $output[2] = $itemprice; Note that first index of an array is 0, not 1. Commented Mar 13, 2014 at 22:23
  • As the variables contain long strings, I didnt want to do it in one line as you suggested. I dont think the problem is with my PHP code if all the other broswers can execute it fine Commented Mar 13, 2014 at 22:26
  • As I said in the OP, I already tried console.log(data) and it returned null in IE, but other browsers it returned the array Commented Mar 13, 2014 at 22:34
  • what does the console.log() says if you remove the datatype:json line? Commented Mar 13, 2014 at 22:36
0

Not quite sure why, try to warp it as a kv object?

$output = array(
    'item' => array (
        $itemname, $itemdescription, $itemprice
    )
);
echo json_encode($output);
exit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.