2

I'm trying to refresh the content of my cells of an table. Therefore, I have a JavaScript which contains an AJAX request to a .php file, which creates my content that I want to insert into my table via JavaScript. The .php files last command is something like echo json_encode($result);.

Back in the JavaScript it says:

var testarray = xmlhttp.response;
alert(testarray);

But the outut from the alert looks like:

{"1":{"1":"3","2":"0","3":"2","4":"0"}}{"1":{"1":"3","2":"0","3":"2","4":"0"},"2":{"1":"2","2":"1","3":"1","4":"1"}}...    

So it seems the variable testarray isn't handled as an array but as a string. I already tried var testarray = JSON.parse(xmlhttp.response), but this doesn’t work. Neither does eval() works.

I don't know what to do, so the response of my request becomes an object.

6
  • what doesn't work with JSON.parse() ? Commented Aug 27, 2013 at 8:41
  • 1
    You encode your results as json, so you get json. You can get arrays, but why would you. JSON is awesome. See this "json to array" thread stackoverflow.com/questions/6872832/json-to-javascript-array Commented Aug 27, 2013 at 8:42
  • @GerbenJacobs JSON can pass arrays too (not only objects). Commented Aug 27, 2013 at 8:58
  • 1
    @oli Make sure you're JSON-encoding a real array on the server side. Commented Aug 27, 2013 at 8:58
  • @Gerben Jacobs my variable build out of the response don't have to be an array. But I'm looking for a way to have access to the single elements of my array I've createt inside my php file. If this works anyhow without forcing my response to be an Array this would be fine, too Commented Aug 27, 2013 at 9:01

2 Answers 2

1

There are 2 strange things in your json:

  1. this part is not json valid: ...}{... two object should be separated by comas

  2. The notation is object with string indexes not array with int indexes it should be something like: [[1,2,3,4],[5,6,7,8]]

for the point 1. it looks like you have a loop that concatenate many json

for the point 2. the object notation can be used as an array so it doesn't matter

Some code:

    //the folowing code doesn't work: }{ is not parsable
var a=JSON.parse('{"1":{"1":"3","2":"0","3":"2","4":"0"}}{"1":{"1":"3","2":"0","3":"2","4":"0"},"2":{"1":"2","2":"1","3":"1","4":"1"}}');

    //the folowing code work and the object can be used as an array
var a=JSON.parse('{"1":{"1":"3","2":"0","3":"2","4":"0"},"2":{"1":"2","2":"1","3":"1","4":"1"}}');
alert(JSON.stringify(a[1]));


    //the folowing code displays the real notation of a javascript array:
alert(JSON.stringify([1,2,3,4]));
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks to your answer. You are right.In the php file, I didn't echo'd my $result array at the end, but inside of a for loop. I changed this and now it works.
0

I think that the problem here might be that your arrays do not have index 0.

e.g. if you output this from the server - it would produce an object:

$result = [];
for ($i = 1; $i < 5; $i++) $result[$i] = $i;
echo json_encode($result);      // outputs an object

if you output this from the server - it would produce an array:

$result = [];
for ($i = 0; $i < 5; $i++) $result[$i] = $i;
echo json_encode($result);     // produces an array

Anyways, even if your server outputs array as object - you should still be able to access it normally in javascript:

var resp = xmlhttp.responseText,  // "responseText" - if you're using native js XHR
    arr = JSON.parse(resp);       // should give you an object
console.log(arr[1]);              // should give you the first element of that object

Comments

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.