App-MFILE-WWW

 view release on metacpan or  search on metacpan

share/js/core/ajax.js  view on Meta::CPAN

//     "path": "logout",
//     "body": null
// }
//
// For details on how AJAX calls are handled, see lib/App/MFILE/WWW/Resource.pm
//
"use strict";

define ([
    'jquery',
    'cf',
    'current-user',
    'html',
    'lib',
], function (
    $,
    cf,
    currentUser,
    html,
    lib,
) {

    var logout = function () {
        currentUser('obj', null);
        currentUser('priv', null);
        appLib.fillUserBox();
        $('#mainarea').html(html.logout());
    }
    ;

    return function (rest, sc, fc) {
        // console.log("Initiating AJAX call", mfao);
        lib.displayResult('* * * AJAX call * * *');
        $.ajax({
            'url': '/',
            'data': JSON.stringify(rest),
            'method': 'POST',
            'processData': false,
            'contentType': 'application/json'
        })
        .done(function (data, textStatus) {
            console.log("REST API RESPONSE", data);
            if (data.level === 'OK') {
                if (typeof sc === 'function') {
                    lib.clearResult();
                    sc(data);
                } else {
                    lib.displayResult(data.text);
                }
                return;
            } else {
                if (typeof fc === 'function') {
                    lib.clearResult();
                    fc(data);
                } else {
                    lib.displayError(data.payload.message);
                }
                return;
            }
            console.log("AJAX unexpected success response ->" + textStatus + "<- with data", data);
            throw textStatus;
        })
        .fail(function (data, textStatus) {
            console.log("AJAX failure", data);
            lib.displayResult(textStatus);
            logout();
        });
    };

});



( run in 0.995 second using v1.01-cache-2.11-cpan-39bf76dae61 )