I am pulling data with $.getJSON and displaying it in a div. I thought I would have another json query to get one product but I don’t so I was wondering of there is anyway to query the results or just grab a few variables to pass? I need to pass one record to another page. I found this on this site:
var people = json.filter(function(el)
{
return el.Phoneno.some(function(number)
{
return number.Cell == "777-777-7777";
});
});
Seems like I need to pull the entire query again though and parse through it. Is there away to just pass a few variables to another page? Very new to jQuery, if someone could point me it right direction that would be great, thanks for any help!
UPDATE:
$.getJSON("myurl", function(data){
$.each(data.myproducts, function(i,item){
$("#products").append("<li><a href='"+item.Url+"' target='_top'><img src='"+item.image+"'></a>"
+"<br>"+item.Name+"<br>"+"$"+item.saleprice+"</li>");
});
});
You can pass a JavaScript object:
through a URL query parameter by serializing it to JSON:
and then URL-encoding it:
and then adding it to the query string of a URL. If you have an anchor like this:
you can modify it with jQuery like this:
The receiving page will then have to unpack the data in the URL, if present.
…or you could just pass some sort of item ID:
Edit
Try this, which uses
$.param(...)instead ofencodeURIComponent(JSON.stringify(...)):This can definitely be cleaned up, but I’ve left it as similar to the original code as possible for clarity. CodeReview.SE is a great resource for improving correct code.
Then, on the receiving page, you need to grab the value of the
dataparameter. I recommend using jQuery BBQ for that: