mirror of
https://github.com/moodle/moodle.git
synced 2025-07-18 21:01:27 +02:00
MDL-50783 Ajax: Configure how to call a webservice through db/service.php
Now the db/service.php array can contain these extra keys to provide information on how a webservice may be called: 'ajax' => true (Default is false) Replaces the xx_is_allowed_from_ajax callback. 'loginrequired' => false (Default is true) Means that this webservice can be called through lib/ajax/service-nosession.php which sets NO_MOODLE_COOKIES to true (faster). This is only safe for webservices returning static public data (e.g. get_string).
This commit is contained in:
@@ -104,9 +104,13 @@ define(['jquery', 'core/config'], function($, config) {
|
||||
* can be attached to the promises returned by this function.
|
||||
* @param {Boolean} async Optional, defaults to true.
|
||||
* If false - this function will not return until the promises are resolved.
|
||||
* @param {Boolean} loginrequired Optional, defaults to true.
|
||||
* If false - this function will call the faster nologin ajax script - but
|
||||
* will fail unless all functions have been marked as 'loginrequired' => false
|
||||
* in services.php
|
||||
* @return {Promise[]} Array of promises that will be resolved when the ajax call returns.
|
||||
*/
|
||||
call: function(requests, async) {
|
||||
call: function(requests, async, loginrequired) {
|
||||
var ajaxRequestData = [],
|
||||
i,
|
||||
promises = [];
|
||||
@@ -144,15 +148,20 @@ define(['jquery', 'core/config'], function($, config) {
|
||||
async: async
|
||||
};
|
||||
|
||||
var script = config.wwwroot + '/lib/ajax/service.php?sesskey=' + config.sesskey;
|
||||
if (!loginrequired) {
|
||||
script = config.wwwroot + '/lib/ajax/service-nologin.php?sesskey=' + config.sesskey;
|
||||
}
|
||||
|
||||
// Jquery deprecated done and fail with async=false so we need to do this 2 ways.
|
||||
if (async) {
|
||||
$.ajax(config.wwwroot + '/lib/ajax/service.php?sesskey=' + config.sesskey, settings)
|
||||
$.ajax(script, settings)
|
||||
.done(requestSuccess)
|
||||
.fail(requestFail);
|
||||
} else {
|
||||
settings.success = requestSuccess;
|
||||
settings.error = requestFail;
|
||||
$.ajax(config.wwwroot + '/lib/ajax/service.php?sesskey=' + config.sesskey, settings);
|
||||
$.ajax(script, settings);
|
||||
}
|
||||
|
||||
return promises;
|
||||
|
Reference in New Issue
Block a user