Search code examples
phpmoodlemoodle-apimoodle-thememoodle-boost

Moodle error: SyntaxError Unexpected token m in JSON at position 0


I've encountered this error in Moodle (v3.11), and was wondering if anyone knows of a fix.

I have a block plugin that I am building with my team. All four of us are getting the same error.

After installing, when I click on the "Add a Block" button in the Moodle Dashboard, it is throwing this error:

SyntaxError
Unexpected token m in JSON at position 0

SyntaxError: Unexpected token m in JSON at position 0
at parse (<anonymous>)
at http://localhost/moodle/lib/javascript.php/1643153825/lib/jquery/jquery-3.5.1.min.js:2:79369
at l (http://localhost/moodle/lib/javascript.php/1643153825/lib/jquery/jquery-3.5.1.min.js:2:79486)
at XMLHttpRequest.<anonymous> (http://localhost/moodle/lib/javascript.php/1643153825/lib/jquery/jquery-3.5.1.min.js:2:82254)

I have narrowed down the code that is causing the error to a simple echo call:

Note: I've been using echo to temporarily display information from the functions I'm developing in the block.

example (throwing the unexpected "m" at position 0).

echo 'mod id: ' . $moduleid . "<br>"; 

While exploring the error, the m in "Unexpected token m in JSON at position 0" always shows the first character in the first echo block in my code, regardless of which or how many echo calls I have in the block.

As this is my first coding job, and I've only been using Moodle and PHP for only about 3 weeks now, I am concerned that I may have done something wrong with my echo call? The error throws when using both 'single' and "double" quotes.

Why would adding an echo to a block plugin cause a syntax error when clicking "Add a Block" in Moodle 3.11?


Solution

  • None of the functions in your block should directly produce any output - they should only return values, that can be output at an appropriate position within the page.

    If you add extra 'echo' statements into your code, then you should expect exactly the sort of issue you are seeing here:

    • The 'Add the block' Javascript code issues a callback to the server to do the adding
    • As part of that process the block's get_content() function is called to see if there is any content to display within the block for the current user (this determines whether or not the block should be displayed for the current user)
    • The server code then outputs a JSON-formatted response, that the Javascript parses to check that the callback was successful.

    If you add any extra output to the block's functions, then this will appear in the response before the expected JSON output. e.g. you might get something like this:

    mod id: 5<br>{"error": false, "response": "...CONTENT..."}
    

    When the Javascript code tries to parse this as JSON, it finds the first character is an 'm' not a '{', so it fails with the error you are reporting.

    If you want to understand / debug your code as you are developing it, you should install xdebug and use an appropriate IDE (I use PHPStorm, but other IDEs work as well) to set breakpoints and step through your code - this allows you to directly see what is going on, without messing with the generated output.