1

I'm trying to build a simple form with a single text field and a submit button that when clicked, posts the value to a $.getJSON method which retrieves the JSON response and outputs it to webpage.

Is there anyway I can do this dynamically using AJAX and PHP? Would I need to use a $.ajax method to POST the value to a PHP page or can I just refractor my existing method.

$.getJSON(
    "https://www.googleapis.com/shopping/search/v1/public/products?callback=?",
    {
        key: "keyhere", 
        country: "US", 
        q: searchterm, 
        alt: "json" 
    },
    function(data) {

        $.each(data.items, function(i, item){

            if (item.product.images.length > 0) // sanity check
            {

            //global variables
            var link = item.product.images[0]['link'];
            var title = item.product.title;

                var listData = "<li>" + title + "</li>" + '<img title="' + title + '" src="' + link + '" />';

                $('#lists').append(listData);

                var img = $("<img/>").attr("src", link);
                $("<a/>").attr({href: link, title: "image title"}).append(img).appendTo("#images");

                console.log(data)
            }
        });
    }
);

EDIT:

Thanks to SBerg413, I managed to change the method to .ajax and it works perfectly, working example below:

var apiKey = "key";
var country = "US";
var apiurl = "https://www.googleapis.com/shopping/search/v1/public/products?callback=?";
var search = 'starwars';

$.ajax({
    url: apiurl,
    dataType: 'jsonp',
    data : 
    {
        key: apiKey, 
        country: country, 
        q: search,
    },
    success: function(data) {

         $.each(data.items, function(i, item){

            if (item.product.images.length > 0) // sanity check
            {

            //global variables
            var link = item.product.images[0]['link'];
            var title = item.product.title;

                var listData = "<li>" + title + "</li>" + '<img title="' + title + '" src="' + link + '" />';

                $('#lists').append(listData);

                var img = $("<img/>").attr("src", link);
                $("<a/>").attr({href: link, title: "Courtesy of James"}).append(img).appendTo("#images");

                console.log(data)
            }
        });


        // console.log(data);
        console.log(data)
    }
});
1
  • What you want to do is easily possible. I just wonder into which concrete problem did you run with it? Commented Oct 10, 2011 at 10:29

1 Answer 1

2

The jQuery getJSON method is just a shorthand for the ajax() method. http://api.jquery.com/jQuery.getJSON/ So, you should be able to use either. However, the getJSON method uses a GET. So, if you specifically want to use a POST, you're better off using the ajax() method.

Sign up to request clarification or add additional context in comments.

3 Comments

Or post() method, which is also a shorthand for ajax()
Thanks. Can I still add the key, country and searchterm parameters in the ajax() method?
Absolutely. You're going to do it just like you're doing it now. Look at the first example in the documentation at where it says 'data: data,'. That's where you put yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.