开发者

JSON.parse not working

开发者 https://www.devze.com 2023-02-19 10:45 出处:网络
I am using AJAX to g开发者_开发问答et values from database. I am using echo json_encode($writers);

I am using AJAX to g开发者_开发问答et values from database. I am using

echo json_encode($writers);

in php.

in jscript when i call

document.getElementById("writer").innerHTML = xmlhttp.responseText;

i get result

[{"name":"demo-1","user_id":"13","writing_level":"","writing_category":"","pri":3},{"name":"Atif Rauf
Alvi","user_id":"12","writing_level":"High
 School","writing_category":"Social
 Sciences,History,Mathematics and
 Economics,Nature,Health and
 Medicine,Creative
 writing","pri":3},{"name":"ffffo","user_id":"14","writing_level":"High
 School,College,Masters","writing_category":"Literature
 and Language,Social
 Sciences,Mathematics and
 Economics","pri":3},{"name":"mariam","user_id":"15","writing_level":"High
 School","writing_category":"Communications
 and Media,Religion and Theology,Life
 Sciences,Creative
 writing","pri":3},{"name":"ddd","user_id":"17","writing_level":"High
 School,College","writing_category":"Literature
 and Language,Art,Social
 Sciences,History,Law","pri":3},{"name":"maria","user_id":"16","writing_level":"High
 School","writing_category":"Art,Social
 Sciences,History,Law,Mathematics and
 Economics","pri":3},{"name":"Muhammad
 Zoyeb","user_id":"11","writing_level":"High
 School,College","writing_category":"Education,Tourism","pri":3},{"name":"wewe","user_id":"10","writing_level":"","writing_category":"","pri":3},{"name":"janea","user_id":"5","writing_level":"","writing_category":"","pri":3},{"name":"shazia","user_id":"4","writing_level":"","writing_category":"","pri":3},{"name":"s","user_id":"6","writing_level":"","writing_category":"","pri":3},{"name":"iuiui","user_id":"8","writing_level":"","writing_category":"","pri":3},{"name":"demo","user_id":"9","writing_level":"","writing_category":"","pri":3},{"name":"arsalan","user_id":"3","writing_level":"","writing_category":"Nature,Education,Health and Medicine,Communications and Media","pri":3}]

which is a valid json object

but when i use

var writer=JSON.parse(xmlhttp.responseText);

i get error can anyone plz help and explain how i can fix this

Thanks

i am using firefox and in firebug it just show error at JSON.parse


Set appropriate content type in php:

header('Content-type: application/json');
echo json_encode($writers);


Double check your json using jsonLint

I quickly copy and pasted your json into it and got some errors - make sure there are no line breaks in the json string as that will invalidate the json.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号