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.

如何在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
    }
});

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:

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

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

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

我更喜欢使用$.ajax()格式,因为我已经习惯了这种格式(没有特别好的理由——只是个人偏好)。

Edit 09/04/2013:

编辑09/04/2013:

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):

在我的问题结束后(因为“太本地化了”),我发现了一个相关的(相同的)问题——有3票反对(糟糕的是我一开始就没有找到它):

Using jquery to make a POST, how to properly supply 'data' parameter?

使用jquery发布文章,如何正确提供“数据”参数?

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:

这很好地回答了我的问题,我发现这样做更容易阅读&我不需要在URL或数据值中手动使用encodeURIComponent()(这是我在bipen的答案中发现的不清楚的地方)。这是因为数据值是通过$.param()自动编码的。以防这对其他人有用,我举了一个例子:

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

    },
    error: function(xhr) {

    }
});

8 个解决方案

#1


192

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的数据选项。您可以使用ajax中的data选项将数据对象发送到服务器,以及定义发送方式的类型(POST或GET)。默认类型是GET方法

Try this

试试这个

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

And you can get the data by

你可以通过

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

更多相关文章

  1. Node.js和Geddy初学者指南 - 第三部分:使用Mongdb持久化你的数据
  2. 图表。js数据数组使用PHP, MySQL。如何从JSON数组定义数据源?
  3. 将JavaScript对象转换为要插入关系数据库的数组
  4. 简单数据的最佳服务器端数据存储方法
  5. JavaScript数据类型的一些注意点(2)
  6. 将对象值传递给指令而不是任何其他数据变量
  7. 如何使用Angular ng-repeat对数据进行分组?
  8. jqGrid 数据加载完事件 loadComplete 使用
  9. Angularjs指令ng-hide在单元格模板中不对网格数据的更改进行绑定

随机推荐

  1. button selector 样式
  2. Android studio图片ERROR: 9-patch image
  3. android手机中图片的拖拉及浏览功能
  4. RelativeLayout
  5. Android Wi-Fi 设置带宽代码流程
  6. inputtype
  7. Android中Dialog对话框
  8. Android自学笔记(番外篇):全面搭建Linux环境
  9. android 瀑布流简单例子
  10. Android总结篇系列:Android 权限