sandrasimon of Cubera
11/29/2016 - 7:50 AM

[JAVASCRIPT] Load json file asynchronously [NEED JQUERY]

[JAVASCRIPT] Load json file asynchronously [NEED JQUERY]

/**
 *
 *  --- HOW TO USE ---
 *
 *
 * Copy/Paste anywhere in your js code
 * and use like this:
 *
 * loadExternalJsonFile('your-file.ext', function(data){
 *   console.log(data);
 * });
 *
 * Example of a json file
 *
 * {
 *    "config": {
 *      "foo": "bar",
 *      "john": "doe"
 *    }
 * }
 *
 */



/**
 * Load json file asynchronously
 * 
 * @require jQuery
 *
 * @param   {string}   file
 * @param   {Function} callback
 */
function loadExternalJsonFile(file, callback) {
    return $.ajax({
        url: file, dataType: 'JSON', 
        jsonp: false, crossDomain: true, 
        global: false, success: callback
    });
}