Raphael D.G
Raphael D.G

Reputation: 260

jquery json load two variables

I have two .json files

data_prod_ind.json

{"articles":
    [ { "title":"bla bla" } , { "title":"bla bla" } ]
}

data_prod_ind_epif.json

{"names":
    [ { "title":"bla bla" } , { "title":"bla bla" } ]
}

html

<div id="mn_1" class="mn_sb" data-articleidx="0">CONTROL RELAY</div>
...
<div id="tt_mn"></div>

<div id="content_mn"></div>

script

var idx = $(this).data('articleidx');
$.getJSON("auth/data_prod_ind.json", function(data) {
    $("#tt_mn").html($("<p class='prod_c'>" + data.articles[idx].title + "</p>"));
    $("#content_mn").html($("<p class='prod_d'>" + data.names[idx].title + "</p>"));
});

I think there would be better only one json file in which it could be:

{"articles":
    [ { "title":"bla bla" } , { "title":"ble ble" } ]
}
{"names":
    [ { "title":"blo blo" } , { "title":"blu blu" } ]
}

And I say that because it only show the content in data_prod_ind.json , not the other one.

EXAMPLE

Upvotes: 0

Views: 215

Answers (3)

francadaval
francadaval

Reputation: 2481

In the getJSON you are opening one file but trying to use both of them.

You must open those separately or better have your all your data in an unique hierarchical JSON Object.

Upvotes: 1

Johan
Johan

Reputation: 35194

Valid JSON combined:

[
    {
        "articles": {
        "title": "ble ble"
        },

        "names": {
        "title": "blu blu"
        }
    }
]

Upvotes: -1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Well there's no reason why you can't have one file with:

{
    "articles":[{"title":"bla bla bla"},{"title":"bla bla bla"}],
    "names":[{"title":"bla bla bla"},{"title":"bla bla bla"}]
}

Upvotes: 3

Related Questions