I am using a .json file and jQuery's $.getJSON
to fill div
s with text, my code for that looks like this:
Code:
$.getJSON("/js/content.json", function (data) {
$(".lander .title").text(data.lTitle);
$(".lander .subtitle").text(data.lSubtitle);
$(".lander .text").html(data.lText).split('\n').join('<br/>');
});
JSON File:
{
"comment": "Landing page",
"lTitle": "Your webpage",
"lSubtitle": "Your webpage subtitle",
"lText": "Edit this text under >js/content.json< ",
"lDetailsTitle": "Here goes the details title under the landing header.",
"lDetailsText": "Here goes the details text."
}
What I am trying to do is use $parseJSON
to check if the whole content.json
is valid (Not just one string). The reason I want to do this is so that jquery can display an error message in the DOM so the user is aware why there is no text in the div
s.
I've tried this, but it is not working:
var isValid = jQuery.parseJSON("/js/content.json");
if (isValid === true) {} else {
$("#overlayMessage").css("display", "block");
$("#overlayMessage .title").text("Oh no, we can't display this page!");
$("#overlayMessage .text").text("An error occured with the file under: 'js/content.json'. Reason: Invalid.");
}
Is this possible to do at all, or can you only check if one string is valid?