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.

Alex
  • 5,565
  • 6
  • 36
  • 57
oli
  • 691
  • 1
  • 8
  • 13
  • what doesn't work with `JSON.parse()` ? – Sirko Aug 27 '13 at 08: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 http://stackoverflow.com/questions/6872832/json-to-javascript-array – Gerben Jacobs Aug 27 '13 at 08:42
  • @GerbenJacobs JSON can pass arrays too (not only objects). – Dziad Borowy Aug 27 '13 at 08:58
  • 1
    @oli Make sure you're JSON-encoding a real array on the server side. – Dziad Borowy Aug 27 '13 at 08: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 – oli Aug 27 '13 at 09:01
  • @tborychowski inside the php file, it says `for($i...){for($j...){$result[$i][$j] = xyz;}}` Could this be the problem? – oli Aug 27 '13 at 09:02

2 Answers2

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]));
pdem
  • 3,880
  • 1
  • 24
  • 38
  • 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. – oli Aug 27 '13 at 09:43
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
Dziad Borowy
  • 12,368
  • 4
  • 41
  • 53