Pawan
Pawan

Reputation: 32321

How to pass parameters properly with AJAX get request

I am making a AJAX get request this way

$(document).on("click", ".topp", function () 
{
 var id_attr_val = $(this).attr("id");
 alert(id_attr_val);
 $.ajax({
            type: 'GET',
            url: 'http://hostip:8080/OrderSnacks/oms/toppings',
            jsonpCallback: 'jsonCallback',
            dataType: 'jsonp',
            data : "topping="+id_attr_val+"",
            jsonp: false,
            success: function (response) {
             console.log(response);
            },
            error: function (e) {
                $("#divResult").html("WebSerivce unreachable");
            }
        });
});

The alert value is being displayed as 59 , but when i observed in the console , its making a call this way

http://hostip:8080/OrderSnacks/oms/toppings?topping=59&_=1402670107061

Could you please tell me how can i send 59 , instead of those extra characters ??

Upvotes: 1

Views: 7956

Answers (3)

Joseph Dailey
Joseph Dailey

Reputation: 4925

Your current code should still work when you go to use the value of toppings as the & separates two different parameters.

After looking throught the jquery docs I found that setting cache:true in your ajax options will remove the _ parameter.

Howerver, you should consider if you actually want to allow caching instead of just removing it. I don't know the scope of your project so that's for you to figure out.

Also if you want to reformat your ajax you have two options.

url: 'http://hostip:8080/OrderSnacks/oms/toppings',

becomes

url: 'http://hostip:8080/OrderSnacks/oms/toppings?topping=' + id_attr_val,

and remove the data memeber

or

data : "topping="+id_attr_val+"",

becomes

data : {topping:id_attr_val},

Upvotes: 1

Shai
Shai

Reputation: 7317

Check the topping value in your server script, you will find it is correctly set to 59.

The extra characters you see are a second variable, called _ being sent with a value of 1402670107061. You can see this by the & character, which is used to separate multiple GET parameters.

As for what this _ variable is for, it is jQuery's cache busting, to ensure the request always goes to the server not the browser's cache. It defaults to true when using dataType: jsonp. See Who Add "_" Single Underscore Query Parameter? and the cache parameter of http://api.jquery.com/jQuery.ajax/. If you want to get rid of it and allow results to be cached, set cache: true on the AJAX options.

Upvotes: 1

PeterKA
PeterKA

Reputation: 24638

To 'remove' the &_=1402670107061 part you would have to set the following in your ajax request:

cache: true,

And, yes, you can still use data: {topping: id_attr_val },.

cache (default: true, false for dataType 'script' and 'jsonp') Type: Boolean If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

Upvotes: 3

Related Questions