Request Json and pass a variable?

I retrieve the data with $.getJSONand display it in a div. I thought that I would have another json request to get one product, but I do not, so I was wondering that in any case, it is possible to request results or just capture several variables? I need to transfer one entry 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";     
   });
}); 

It looks like I need to pull out the entire request again and analyze it. Is it possible to skip several variables to another page? Very new to jQuery, if someone could point me in the 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>");
    });
});
+3
source share
1 answer

You can pass a JavaScript object:

var data = {
    url: 'http://example.com',
    image: 'http://placekitten.com/200/300',
    name: 'can haz cheezburger?'
};

URL, JSON:

var strData = JSON.stringify(data);
// '{"url":"http://example.com","image":"http://placekitten.com/200/300","name":"can haz cheezburger?"}'

URL-:

var encodedStrData = encodeURIComponent(strData);
// '%7B%22url%22%3A%22http%3A%2F%2Fexample.com%22%2C%22image%22%3A%22http%3A%2F%2Fplacekitten.com%2F200%2F300%22%2C%22name%22%3A%22can%20haz%20cheezburger%3F%22%7D'

URL-. :

<a id="next" href="next/page">Next Page</a>

jQuery :

var $next = $('#next'),
    url = $next.prop('href') + '?data=' + encodedStrData;

$next.prop('href', url);

URL-.


... - :

<a href="next/page?item=42">Next Page</a>

, $.param(...) encodeURIComponent(JSON.stringify(...)):

$.getJSON("myurl", function(data)
{
    $.each(data.myproducts, function(i,item)
    {
        var url = 'details.html?' + $.param({data: item});

        $("#products").append("<li><a href='"+url+"' target='_top'><img src='"+item.image+"'></a>"
               +"<br>"+item.Name+"<br>"+"$"+item.saleprice+"</li>");
    });
});

, . CodeReview.SE - .

data. jQuery BBQ :

$(function ()
{
    var item = $.deparam.querystring().data;
    // Item is now one of the items from the JSON on the previous page.
    // Do whatever you need with it
});
+3

All Articles