javascript variable equal to .json file content

1

I currently have a .js in which I save a json array and equal it to a variable

applications = {"apps": [
{ "app":"app1", "buzon":"buzon1" },
...
]};

And below, in the same script I use the variable to work with the json data. The idea is to remove the array from the .js file to clarify the code, save it in another .json file and from the .js invoke its contents and match it to the variable. I hope you have explained to me.

Can someone tell me how to do this?

Thank you very much:)

    
asked by Alex Alvarez 08.07.2018 в 10:45
source

1 answer

0

I think that if you wanted to extract the array from the javascript file you would sacrifice the performance, because you would have to obtain the data after having loaded the script, but what I would do after having removed the fix and put it in a .json file would only be get it with ajax, for example using JQUERY would be:

$.ajax({
        // ruta relativa o absoluta de donde se encuentra el fichero o archivo
        url: "data.json",
        type: "GET",
        dataType: 'json',
        success: function(data) {
            // hacer tu logica ya teniendo la informacion del json
        },
        error: function(datas) {
            // logica si falla la carga
        }
});
    
answered by 08.07.2018 в 15:03