Welcome to WebmasterWorld Guest from 54.145.94.234

Forum Moderators: ergophobe

Message Too Old, No Replies

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

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

New User

joined:Feb 14, 2016
posts:18
votes: 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]

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

Moderator This Forum

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8476
votes: 222


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)?
9:23 pm on Feb 16, 2016 (gmt 0)

New User

joined:Feb 14, 2016
posts:18
votes: 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"
});
9:36 pm on Feb 16, 2016 (gmt 0)

Moderator This Forum

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8476
votes: 222


Glad you got it sorted!

(and thanks for the tip)