如何使用 jQuery 在 GET 请求中传递参数 [英] How to pass parameters in GET requests with jQuery

查看:102
本文介绍了如何使用 jQuery 在 GET 请求中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How should I be passing query string values in a jQuery Ajax request? I currently do them as follows but I'm sure there is a cleaner way that does not require me to encode manually.

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

我见过查询字符串参数作为数组传递的示例,但我见过的这些示例没有使用 $.ajax() 模型,而是直接转到 $.get().例如:

I’ve seen examples where query string parameters are passed as an array but these examples I've seen don't use the $.ajax() model, instead they go straight to $.get(). For example:

$.get("ajax.aspx", { UserID: UserID , EmailAddress: EmailAddress } );

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

I prefer to use the $.ajax() format as it's what I’m used to (no particularly good reason - just a personal preference).

编辑 09/04/2013:

在我的问题结束后(如太本地化"),我发现了一个相关(相同)的问题 - 不少于 3 个赞成票(我的坏处是一开始没有找到它):

After my question was closed (as "Too Localised") i found a related (identical) question - with 3 upvotes no-less (My bad for not finding it in the first place):

使用jquery制作一个POST,如何正确提供'data'参数?

这完美地回答了我的问题,我发现这样做更容易阅读&我不需要在 URL 或 DATA 值中手动使用 encodeURIComponent() (这是我在 bipen 的回答中发现不清楚的).这是因为 data 值是通过 $.param()).以防万一这对其他人有用,这是我使用的示例:

This answered my question perfectly, I found that doing it this way is much easier to read & I don't need to manually use encodeURIComponent() in the URL or the DATA values (which is what i found unclear in bipen's answer). This is because the data value is encoded automatically via $.param()). Just in case this can be of use to anyone else, this is the example I went with:

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

    },
    error: function(xhr) {

    }
});

推荐答案

使用ajax的数据选项.您可以通过 ajax 中的 data 选项和定义发送方式的 type 将数据对象发送到服务器(POST获取).默认类型是GET方法

Use data option of ajax. You can send data object to server by data option in ajax and the type which defines how you are sending it (either POST or GET). The default type is GET method

试试这个

$.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
  }
});

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

And you can get the data by (if you are using PHP)

 $_GET['ajaxid'] //gives 4
 $_GET['UserID'] //gives you the sent userid

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

In aspx, I believe it is (might be wrong)

 Request.QueryString["ajaxid"].ToString(); 

这篇关于如何使用 jQuery 在 GET 请求中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆