Solarpitch Posted November 5, 2012 Share Posted November 5, 2012 (edited) Hey Guys, I'm sending a php array in JSON to a JS function like this: $arr= array( 'a' => "Gerard", 'b' => "Jonathan" ); echo json_encode($arr); I can see it's being sent like: {"a":"Gerard","b":"Jonathan"} However, when I try and retrieve it in the javascript function below I get an error: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data obj = JSON.parse(response); $.post('http://site.comi/ajaxApiCall', 'val=' + val, function (response) { obj = JSON.parse(response); alert(obj['a']); }); Still get the error even if I dont use JSON.parse. Just not sure where I'm going wrong. Edited November 5, 2012 by Solarpitch Quote Link to comment https://forums.phpfreaks.com/topic/270332-php-to-json-little-issue/ Share on other sites More sharing options...
Christian F. Posted November 5, 2012 Share Posted November 5, 2012 Tried trimming the response, before parsing it as JSON? Should fix the problem. Also, sounds like you might have some whitespaces after the closing PHP tag in the file that generates the JSON response. Quote Link to comment https://forums.phpfreaks.com/topic/270332-php-to-json-little-issue/#findComment-1390436 Share on other sites More sharing options...
Solarpitch Posted November 6, 2012 Author Share Posted November 6, 2012 No whitespace after PHP tag also tried: var res = $.trim(response); obj = JSON.parse(res); but still seem to get the same message,. Quote Link to comment https://forums.phpfreaks.com/topic/270332-php-to-json-little-issue/#findComment-1390561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.