Answer the question
In order to leave comments, you need to log in
AJAX filtering of the catalog with goods. How to insert a response into a page?
As we already know, a product can have properties such as: price, color, manufacturer, and the like. AJax request is sent by choice selecta is able to. I am familiar with ajax requests. But it is not possible to correctly insert the received answer into the page. And in general, when I look at the answer in the Network tab in the Google Chrome developer console, the whole page comes in response, that is, its entire html code. I understand that it can be returned in JSON format, but as I said, the problem for me is to insert the response into the page.
Answer the question
In order to leave comments, you need to log in
Check on the server side whether a normal or XMLHTTPRequest request has arrived. If an XMLHTTPRequest request comes in, then don't return the entire page, but only what you need to replace. How to insert the answer on the page I think it is not necessary to explain?
Either manually create html markup from the JSON response, or you can use template engines like handlebars.js . Search keywords for "javascript template engine".
Quickly found a video:
YouTube
Try it.
From myself I’ll say that I would rather receive data in json format and through each I would collect a list for display
Logic:
We got json of the form -
{
"a":{
"price":1000,
"size":10
},
"b":{
"price":1000,
"size":10
},
"c":{
"price":1000,
"size":10
}
}
$(".list-items").html('');
$.each(json, function(i, data){
$(".list-items").append("<li><span class='price'>" + data.price + "</span><span class='size'>" + data.size + "</span></li>");
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question