1

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?

Kurt_eh
  • 49
  • 2
  • 10
  • `echo 'mod id: ' . $moduleid . "
    "; ` would not put out JSON. Look at https://www.php.net/manual/en/function.json-encode.php
    – user3783243 Jan 26 '22 at 01:12
  • All my echo calls were doing what they're supposed to do, and printing to the window. As a new programmer, I defer to you that it shouldn't put out to JSON. Perhaps one of the things it's showing in the error message is calling JSON? It even throws the error on a line where I echo "
    "; at the top of the page!
    – Kurt_eh Jan 26 '22 at 01:16

1 Answers1

1

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.

davosmith
  • 6,037
  • 2
  • 14
  • 23
  • Thank you. We've been using Visual Studio. Will have to seek out xdebug or similar for it. – Kurt_eh Jan 26 '22 at 14:54
  • One further rookie developer question, if I make a DB call, can I display the data from a block in the console? – Kurt_eh Jan 26 '22 at 16:24
  • If you have xdebug working, then you can examine the results of the DB call by setting a breakpoint and examining the results in the variable they are assigned to. You can't easily output the results to the javascript console in your browser. – davosmith Jan 26 '22 at 18:52