Forum Moderators: open

Message Too Old, No Replies

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of th

         

oligalma

11:14 pm on Feb 14, 2016 (gmt 0)



I'm writing a Joomla component and I'm trying to do ajax request but I always get this error (SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data). I cannot understand, as I'm sure the response is JSON:

$response = array();
// ...
echo json_encode($response);


<snip>

[edited by: ergophobe at 8:52 pm (utc) on Feb 15, 2016]
[edit reason] Please no URLs to personal sites [/edit]

ergophobe

9:00 pm on Feb 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The unexpected character is line 1, col 1, so it should be the first character in the string you're sending to be parsed as JSON
- is it a legal character?
- is it possibly a hidden control character?

Can you dump the data before attempting to parse it and then check it against a JSON validator (JSLint or what have you)?

oligalma

9:23 pm on Feb 16, 2016 (gmt 0)



Finally I could fix the error! It's weird, I had to add "format=raw" to the ajax url:

 jQuery.ajax({
data: {letter: jQuery(this).text()},
type: "POST",
dataType: "json",
url: "index.php?option=com_hangman&task=guess&format=raw"
});

ergophobe

9:36 pm on Feb 16, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you got it sorted!

(and thanks for the tip)