General developer forum

How to debug SyntaxError JSON.parse: unexpected character... ?

 
Picture of Ludo M
How to debug SyntaxError JSON.parse: unexpected character... ?
 

Hello,

I am developping an enrolment plugin which do some action when \core\event\user_enrolment_created $event is trigered and I have a problem :

When I enrol a user from /enrol/manual/manage.php?enrolid=1, it works but when I enrol from moodle/enrol/users.php?id=2 I have this error:

SyntaxError

JSON.parse: unexpected character at line 1 column 1 of the JSON data
Line: 24483
I understand that it is a Json error, but I don't understand where is the bug and how to see this json (I have activated the debug options in Moodle and open the dev console in Firefox), could you please give me hint about debbuging this

 
Average of ratings: -
Picture of Kritika Sood
Re: How to debug SyntaxError JSON.parse: unexpected character... ?
 

I am experiencing the same problem.

Did you get any solution ?

 
Average of ratings: -
Picture of Shadow Adv
Re: How to debug SyntaxError JSON.parse: unexpected character... ?
 
I may have found the cause for this bug. I have just installed a fresh install and also got a smiliar error. Here is my console output:

POST XHR (HOSTNAME)moodle/lib/ajax/service.php
jQuery.Deferred exception: JSON.parse: unexpected character at line 1 column 1 of the JSON data Nb@(HOSTNAME)moodle/lib/javascript.php/1500984545/lib/jquery/jquery-3.1.0.min.js:1:74037
A@(HOSTNAME)moodle/lib/javascript.php/1500984545/lib/jquery/jquery-3.1.0.min.js:1:77562
.send/c/<@(HOSTNAME)moodle/lib/javascript.php/1500984545/lib/jquery/jquery-3.1.0.min.js:1:80166
 undefined jquery-3.1.0.min.js:1:31135
GET (HOSTNAME)moodle/theme/yui_combo.php
GET (HOSTNAME)moodle/theme/yui_combo.php
jQuery.Deferred exception: JSON.parse: unexpected character at line 1 column 1 of the JSON data Nb@(HOSTNAME)moodle/lib/javascript.php/1500984545/lib/jquery/jquery-3.1.0.min.js:1:74037
A@(HOSTNAME)moodle/lib/javascript.php/1500984545/lib/jquery/jquery-3.1.0.min.js:1:77562
.send/c/<@(HOSTNAME)moodle/lib/javascript.php/1500984545/lib/jquery/jquery-3.1.0.min.js:1:80166
 undefined


Then I checked the request previous of the one with error: /moodle/lib/ajax/service.php. The full request was a POST to (HOSTNAME)moodle/lib/ajax/service.php?sesskey=(masked)&info=core_fetch_notifications that returned:

<br />
<b>Deprecated</b>:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in <b>Unknown</b> on line <b>0</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent in <b>Unknown</b> on line <b>0</b><br />
[{"error":false,"data":[]}]

Gotcha! "<br />" isn't a valid JSON string and apparently it made the JSON parser complain. Now how to fix... either disabling php error warning or find a way to fix that deprecated stuff error.

About how to debug that: I used the Network tab of the Dev tools in Firefox to check the result of the request. Just click a request in the Console (must have Net filter enabled) and it takes you straight to it.
 
Average of ratings: -
Picture of Matteo Scaramuccia
Re: How to debug SyntaxError JSON.parse: unexpected character... ?
Core developersParticularly helpful MoodlersPlugin developers

Hello,
in your specific case:

Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead.

you've installed an old version of Moodle in a modern PHP environment e.g. Moodle 2.9- running on PHP 7.0+.

That's the reason why you've have that log message on top of the real JSON payload: [{"error":false,"data":[]}].
Tech details: https://docs.moodle.org/29/en/PHP#HTTP_RAW_POST_DATA_errors and http://php.net/manual/en/migration56.deprecated.php#migration56.deprecated.raw-post-data

HTH,
Matteo

 
Average of ratings: -
Picture of Shadow Adv
Re: How to debug SyntaxError JSON.parse: unexpected character... ?
 

Hi Matteo!

you've installed an old version of Moodle in a modern PHP environment e.g. Moodle 2.9- running on PHP 7.0+.

In my case, it's exactly the opposite. I have instaled what seemed to be the latest version of Moodle, 3.3.1, freshly downloaded from download.moodle.org, in a server running PHP 5.6.19. I apologize, I should have include version info in my reply.

Anyway, setting "always_populate_raw_post_data" as stated by the PHP warning caused the popup with the error to stop showing up, although I still see that error in the console occasionally.


 
Average of ratings: -