开发者

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

How do I pass data to a $.ajax() call? I'd like to pass a GUI开发者_运维百科D (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);
                }
            })
}


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' ] },
    // ...
});


Can't you do it with a query string?

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


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜