DenaliHardtail
DenaliHardtail

Reputation: 28336

How do I pass arguments to $.ajax()?

How do I pass data to a $.ajax() call? I'd like to pass a GUID (string) and, in another case, an array containing an integer and a string.

The code below works fine but I need to pass some arguments for processing. Thanks!

function btnAdd_onclick() {
            $.ajax({
                "url": "Add.aspx",
                "type": "get",
                "success": function (response) {
                    alert(response);
                    $("body").append(response);

                },
                "error": function (response) {
                    alert("Error: " + response);
                }
            })
}

Upvotes: 2

Views: 5050

Answers (3)

mu is too short
mu is too short

Reputation: 434975

You use the data parameter in the options hash:

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

For example:

$.ajax({
    url: '/pancakes/house',
    data: { 'where': [ 'is' ] },
    // ...
});

Upvotes: 3

Alnitak
Alnitak

Reputation: 340055

The preferred method is using the data parameter with an object containing the key-value pairs, e.g.

$.ajax({
    url: ...
    type: ...
    data: {
        guid: ...
        param2: ...
    },
    ...
});

jQuery will wrap that object up into key1=value1&key2=value2 etc format.

If any of the values are arrays then the processing is slightly different - see http://api.jquery.com/jQuery.param/

Upvotes: 0

Jack Marchetti
Jack Marchetti

Reputation: 15754

Can't you do it with a query string?

"url": "Add.aspx" + '?ID=' + [VARIABLE],

Upvotes: 0

Related Questions