AJAX Request

Codoforum provides a wrapper to make secure AJAX requests using jQuery ajax.

Simple GET/POST request

The bare minimum to send a GET/POST request using request object is

    url: 'your_url_here' //Must be an absolute url

Above will send a GET request to 'your_url_here'.
Please refer to codo_defs object for some helful constants to help create your url.

Similarly, to make a POST request use the method CODOF.request.post

If you have a request for example.

    url: codo_defs.url + 'voteUp'

Then, in the server-side, you can capture this request in your plugin by using

Request::get('voteUp', function() {

    //Here comes your code to vote up the user

You can even use wildcards here, for example.

Request::get('voteUp/:id', function($id) {

    //Here comes your code to vote up the user

CSRF is automatically handled by the Request class


Codoforum provides you with CODOF.request.get, CODOF.request.post and CODOF.request.req methods for making AJAX requests.
All above methods accept an options object.

The only required option in the options object is url, which was shown in the previous section.


This option can be used to create requests of type other than GET/POST.

For eg. To create a PUT request, you can use

    type: 'PUT'

Infact, the methods CODOF.request.get and CODOF.request.post both call the method CODOF.request.req with the type 'GET' and 'POST' respectively.


This option is used to send 'data' with your request.



    url: 'your_url',
    data: {
      any: 'js object'


This option is used to define a method which is called after the request is completed.



    url: 'your_url',
    done: function(response) {

        //do the magic with response object

All the standard jQuery methods 'done', 'fail' & 'always' can be used.


This option can be used to allow other AJAX requests in your own plugin or other plugins to modify the data being sent or response obtained from that request.


    hook: 'get_rank',
    data: {'users':  ['usernameOne']},
    done: function(ranks) {

        console.log(ranks); //do something with it.

Now, before the request is made the hook 'before_req_get_rank' is called.
And on request complete, the hook 'on_req_get_rank' is called.

So, you can use them to manipulate your request and response somewhere else in your code.

Before reading further, you need to know about hooks in Codoforum.

Modifying the data just before request is sent

CODOF.hook.add('before_req_get_rank', function(data) {

    var newUsers = data.users.concat(['usernameTwo']); 
    var newData = {users: newUsers};
    return newData;

Note: The modified data must be returned in the function.

Using the request's response

CODOF.hook.add('on_req_get_rank', function(response) {

    console.log(ranks); //ranks of both usernameOne and usernameTwo


This option can be used to prevent any duplicate requests.

Note: The hook option must be defined to uniquely identify your request

When preventParallel is set to true, Codoforum will prevent the request to be made again until the previous request is completed.

© CODOFORUM 2023. All Rights Reserved.