There is a function that sends an Ajax request to the JSON file for content, parses it, compiles it with the Handlebars Template, and places it in the desired div on the html page. How to “teach” it not to take the entire content of the JSON file, but only a specific object / array or element of an object / array (so that you can then replace this part of the code with a variable that you set when calling a function)

Ajax and XMLHttpRequest just started to learn, I will be glad to advice.

Here is the function (framed as commonjs module):

module.exports = function(jsonDir, templId, finId){ function sendGet(callback) { /* create an AJAX request using XMLHttpRequest*/ var xhr = new XMLHttpRequest(); /* Specify the type of request by using XMLHttpRequest "open", here 'GET'(argument one) refers to request type "jsonDir" (argument two) refers to JSON file location*/ xhr.open('GET', jsonDir); /*Using onload event handler you can check status of your request*/ xhr.onload = function () { if (xhr.status === 200) { callback(JSON.parse(xhr.responseText)); } else { alert(xhr.statusText); } }; /*Using onerror event handler you can check error state, if your request failed to get the data*/ xhr.onerror = function () { alert("Network Error"); }; /*send the request to server*/ xhr.send(); } //For template-1 var dateTemplate = document.getElementById(templId).innerHTML; var template = Handlebars.compile(dateTemplate); sendGet(function (response) { document.getElementById(finId).innerHTML += template(response); }) } 

  • If you use asp.net or an analogue on the server side, then you can process (cut the required piece) the required file on the server and send the result to the client. Otherwise, in any case, you will receive a whole file and you will have to select the desired piece on the client after JSON.parse(xhr.responseText)) . - Andrew B
  • @AndrewB I use NodeJS, in this case it can be done on the server? And if I process it on the client, do I understand correctly that after JSON.parse(xhr.responseText)) xhr variable stores JSON content in JS format and Now you can refer, for example, to the desired object or array via xhr.objectName.propertyName ? - Rumata
  • Since nodejs is not familiar, if it allows you to create handlers for requests and work with files, then there should be no problems. Just catch the desired request and make the answer that you need. On the client after var resp = JSON.parse(response); You get a ready object that you can work with as with any other js object var x = resp.setting1 || "default"; var x = resp.setting1 || "default"; - Andrew B
  • @AndrewB Sorry, I did not understand your last example, or rather its second part. In the first you refer to the key or parameter js of the object, and OR "default" - the value that is substituted if there is no such element? What is the "default"? - Rumata
  • This is just for example. Take the value from setting1 if it is otherwise write the string "default". With such a check, the пустая строка, null, undefined, 0, false will not pass it (the string after || will be used instead). This is not a very good way because of the features, but short. - Andrew B

0