javascript - Encode - Decode Json issue -


i try connect chrome extension php file xmlhttprequest.

work fine, can use json.parse decode json.

js file:

var client = new xmlhttprequest();                 client.onreadystatechange = function() {                     if (client.readystate == xmlhttprequest.done) {                             var j = client.responsetext;                          console.log(j);                              }                 }                 client.open("get", "http://localhost/extencio/index.php?"+o, true);                  client.send(); 

in php file return object with:

echo json_encode($ofinal); 

the result of console.log(j); is:

{"textodonde":"nombre","value":"gettext","donde":"name"}{"textodonde":"apellido","value":"gettext","donde":"name"}{"textodonde":"sexo","value":"type no detectoado","donde":"name"} {"textodonde":"sexo","value":"type no detectoado","donde":"name"}

i change line in js file decode:

var j = json.parse(client.responsetext); 

but have other error:

uncaught syntaxerror: unexpected token {client.onreadystatechange @ popup.js:66 vm71:1 uncaught syntaxerror: unexpected token {client.onreadystatechange @ popup.js:66

66 "var j" line.

share|improve question
1  
one single console.log outputting multiple json objects?! echo json_encodeing in loop? you're not sending valid json! send [] array if want send multiple values. – deceze jan 27 '16 @ 15:45
    
you should add objects php array. output json enconding of it's array. – filipe jan 27 '16 @ 15:45
    
@phyron should post php code here too. – styfle jan 27 '16 @ 16:25
up vote 1 down vote accepted

your json output invalid. have several objects in output. want array of objects? try this:

[{"textodonde":"nombre","value":"gettext","donde":"name"},{"textodonde":"apellido","value":"gettext","donde":"name"},{"textodonde":"sexo","value":"type no detectoado","donde":"name"},{"textodonde":"sexo","value":"type no detectoado","donde":"name"}] 

run json through linter http://jsonlint.com/ see if valid.

share|improve answer

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments