handling moodle exceptions with ajax call

handling moodle exceptions with ajax call

by Adi Michan -
Number of replies: 4

Hi,

I have a js file that makes an ajax call to php file.

In the php file I check the user session, and when its throws an exception, I want to present it nice like moodle does with the 'core/notification' module , but Its not working for me.

here is the js code:

$.post(cfg.wwwroot+'/ajax.php', params, function(){})

                                .done(function(data) {

                                    data = JSON.parse(data);

                                    console.log(data);

                                })

                                .fail(function(jqXHR, status, error){

                                    notification.exception(jqXHR);

                                });

and this is the php function that throws the exception:

require_sesskey();

I whant something like this:

c


Thanks in advance,

Adi


Average of ratings: -
In reply to Adi Michan

Re: handling moodle exceptions with ajax call

by Sam Chaffee -
Picture of Core developers

Do you have the following at the top of your PHP script that handles the AJAX request:

define('AJAX_SCRIPT', true);
In reply to Sam Chaffee

Re: handling moodle exceptions with ajax call

by Adi Michan -

no, it was missing, and its looks good now,

thanks!

In reply to Adi Michan

Re: handling moodle exceptions with ajax call

by Damyon Wiese -

You need to pass the error and not the jqXHR to notification.error.

But please, please, please do not invent new AJAX entry points and do not use the JQuery ajax function directly - they are very difficult to use securely and you will get it wrong. 

Do this: https://docs.moodle.org/dev/AJAX

In reply to Damyon Wiese

Re: handling moodle exceptions with ajax call

by Adi Michan -

I tried to pass the error, but it didn't work as well, the error is empty.

Thank you very much on your important notice, I will try it.

Adi