如何使用jQuery在GET请求中传递参数

Hea*_*ore 229 javascript ajax jquery get

我应该如何在jQuery Ajax请求中传递查询字符串值?我目前按照以下方式执行它们,但我确信有一种更简洁的方法,不需要我手动编码.

$.ajax({
    url: "ajax.aspx?ajaxid=4&UserID=" + UserID + "&EmailAddress=" + encodeURIComponent(EmailAddress),
    success: function(response) {
        //Do Something
    },
    error: function(xhr) {
        //Do Something to handle error
    }
});
Run Code Online (Sandbox Code Playgroud)

我已经看到了查询字符串参数作为数组传递的示例,但是我见过的这些示例不使用该$.ajax()模型,而是直接进入$.get().例如:

$.get("ajax.aspx", { UserID: UserID , EmailAddress: EmailAddress } );
Run Code Online (Sandbox Code Playgroud)

我更喜欢使用$ .ajax()格式,因为它是我习惯的(没有特别好的理由 - 仅仅是个人偏好).

编辑09/04/2013:

在我的问题被关闭后(作为"Too Localized"),我发现了一个相关的(完全相同的)问题 - 3个upvotes no-less(我不喜欢首先找到它):

使用jquery进行POST,如何正确提供'data'参数?

这完全回答了我的问题,我发现这样做更容易阅读,我不需要手动使用encodeURIComponent()URL或DATA值(这是我在bipen的答案中发现的不清楚).这是因为该data值是通过自动编码的$.param().为了防止这对任何人都有用,这就是我的例子:

$.ajax({
    url: "ajax.aspx?ajaxid=4",
    data: { 
        "VarA": VarA, 
        "VarB": VarB, 
        "VarC": VarC
    },
    cache: false,
    type: "POST",
    success: function(response) {

    },
    error: function(xhr) {

    }
});
Run Code Online (Sandbox Code Playgroud)

bip*_*pen 280

使用ajax的数据选项.您可以通过dataajax中的选项将数据对象发送到服务器,并type定义您发送它的方式(POST或者GET).默认类型是GET方法

试试这个

$.ajax({
  url: "ajax.aspx",
  type: "get", //send it through get method
  data: { 
    ajaxid: 4, 
    UserID: UserID, 
    EmailAddress: EmailAddress
  },
  success: function(response) {
    //Do Something
  },
  error: function(xhr) {
    //Do Something to handle error
  }
});
Run Code Online (Sandbox Code Playgroud)

你可以通过(如果你使用PHP)获取数据

 $_GET['ajaxid'] //gives 4
 $_GET['UserID'] //gives you the sent userid
Run Code Online (Sandbox Code Playgroud)

在aspx中,我相信它(可能是错的)

 Request.QueryString["ajaxid"].ToString(); 
Run Code Online (Sandbox Code Playgroud)

  • 你不需要`encodeURIComponent`.jquery会为你做这件事. (2认同)
  • @KlwWallace 不。那是 PHP(服务器端)取决于您使用的服务端语言。在 PHP 中,这就是我们如何获取 get 请求的值。 (2认同)
  • @bipen。收到。我删除了我的评论,以免让任何人感到困惑。谢谢。 (2认同)

Cia*_*ims 20

把你的参数放在通话的data一部分ajax.查看文档.像这样:

$.ajax({
    url: "/TestPage.aspx",
    data: {"first": "Manu","Last":"Sharma"},
    success: function(response) {
        //Do Something
    },
    error: function(xhr) {
        //Do Something to handle error
    }
});
Run Code Online (Sandbox Code Playgroud)


the*_*guy 7

Here is the syntax using jQuery $.get

$.get(url, data, successCallback, datatype)
Run Code Online (Sandbox Code Playgroud)

So in your case, that would equate to,

var url = 'ajax.asp';
var data = { ajaxid: 4, UserID: UserID, EmailAddress: EmailAddress };
var datatype = 'jsonp';

function success(response) {
// do something here 
}

$.get('ajax.aspx', data, success, datatype)
Run Code Online (Sandbox Code Playgroud)

Note $.get does not give you the opportunity to set an error handler. But there are several ways to do it either using $.ajaxSetup(), $.ajaxError() or chaining a .fail on your $.get like below

$.get(url, data, success, datatype)
 .fail(function(){
})
Run Code Online (Sandbox Code Playgroud)

The reason for setting the datatype as 'jsonp' is due to browser same origin policy issues, but if you are making the request on the same domain where your javascript is hosted, you should be fine with datatype set to json.

If you don't want to use the jquery $.get then see the docs for $.ajax which allows room for more flexibility