AJAX_SCRIPT session not accepted

AJAX_SCRIPT session not accepted

by Jacob Shapiro -
Number of replies: 5

Hello everyone,

I'm trying to create a block plugin for moodle which gets continual updates via ajax calls.

To this end I have tried employing built in functionality in moodle: Requesting lib/ajax/blocks.php in order to get the contents of one particular block. But this functionality seems to not be implemented (I deduce this from reading the source code of lib/ajax/blocks.php Please correct me if I'm wrong).

My next step was to try to create my own PHP script which would cater to my javascript code and provide the block contents in JSON format. However, very early I got stuck with the following problem:

{"error":"Incorrect sesskey submitted, form not accepted!"}

For a very simple PHP script which has exactly three lines:

<?php

define('AJAX_SCRIPT', true);

require_once(dirname(__FILE__) . '/../../config.php');

require_sesskey();

 

Thinking I might've messed up the javascript code, I decided to try to query another AJAX_SCRIPT from moodle, namely, comment/comment_ajax.php. This, however worked (well, it passed the session test and failed somewhere else, but the point is the session test worked).

Desperate to find the problem, I copied (verbatim) the source file from comment/comment_ajax.php into my block-plugin directory, changed only the include paths so that they would work, and BOOM, getting "Incorrect sesskey submitted" again. Seems like the path where the AJAX_SCRIPT sits somehow affects the session verification???

I even made a duplicate of comment/comment_ajax2.php (same file exactly, just a different path) and it passed the session test. It's as if there is something about my block-plugin directory that moodle doesn't want to recognize as a valid session.

Does anyone have any idea what I'm doing wrong?

In case anyone wants to look, here are the source files:

https://github.com/jacobshapiro/moodle-block_eduappup/tree/ajax

(note: I know the javascript file is a mess and I will fix that, but as I explained above, it is probably not the cause to the session problem)

Thank you very much in advance!

Average of ratings: -
In reply to Jacob Shapiro

Re: AJAX_SCRIPT session not accepted

by Darko Miletić -

You are not passing the session key in your ajax call. That is the error. Add sesskey parameter to the url with M.cfg.sesskey value.

In reply to Darko Miletić

Re: AJAX_SCRIPT session not accepted

by Jacob Shapiro -

Dear Darko,

Have a look at line 127 of javascript.js:

https://github.com/jacobshapiro/moodle-block_eduappup/blob/27c43c4c0dda9b796f1b579e81bf48c3d4b971f3/javascript.js#L127

You will see I did exactly that originally. Otherwise, the call to the built in comment/comment_ajax.php would not have worked!

 

Thanks again.

In reply to Jacob Shapiro

Re: AJAX_SCRIPT session not accepted

by Darko Miletić -

Your code needs to include dependency querystring-stringify-simple which is used to encode parameters. 

In reply to Darko Miletić

Re: AJAX_SCRIPT session not accepted

by Jacob Shapiro -

Dear Darko, thank you for your response. 

If you'll look here: 

https://github.com/jacobshapiro/moodle-block_eduappup/blob/27c43c4c0dda9b796f1b579e81bf48c3d4b971f3/javascript.js#L27

On line 27, you'll see that I indeed include this dependency.

In fact, debugging the AJAX_SCRIPT php file at run time indicates that the sesskey parameter arrives properly to the script from the JS call.

 

Sincerely,

~Jacob

In reply to Jacob Shapiro

Re: AJAX_SCRIPT session not accepted

by Darko Miletić -

You are not including that dependency. This is the line in question:

varNDY=YUI().use("node","io",'json-parse','json-stringify',function(Y){

I do not see the querystring-stringify-simple on that list. json-stringify is something different than querystring-stringify-simple.