c# - 在 jquery 中将数组作为查询字符串传递

标签 c# jquery asp.net query-string

我在名称为 array[] 的 jquery 中创建了一个字符串数组,并将值推送给它。

如何在查询字符串中发送这个数组?

例如window.location.href('ViewData?array');

以及如何从 jquery 本身的查询字符串中取回数组数据? 请指教?

最佳答案

这很容易用 jQuery 实现,因为它有一个名为 param() 的辅助函数。

var myData = {
   name: "John Doe",
   age: 23,
   children: [{
      name: "Jane Doe",
      age: 13,
      children: []
      },
      {
      name: "John Doe Jr.",
      age: 16,
      children: []
      }
   ],
}
var p = $.param(myData)

结果:

"name=John+Doe&age=23&children%5B0%5D%5Bname%5D=Jane+Doe&children%5B0%5D%5Bage%5D=13&children%5B1%5D%5Bname%5D=John+Doe+Jr .&children%5B1%5D%5Bage%5D=16"

你可以这样使用它:

window.location = "/MyController/MyAction?" + p;

关于从查询字符串中获取参数,请引用以下问题:How can I get query string values in JavaScript?


您可以做的另一件事是使用 ajax 调用,这样您就不必自己序列化数据,因为它会自动为您完成。

$.ajax({
  url: "/MyController/MyAction",
  data: myData,
  dataType: "json",
  type: "POST",
  success: function(data){
    //'data' is your response data from the Action in form of a javascript object
  }
})

关于c# - 在 jquery 中将数组作为查询字符串传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25440902/

相关文章:

C# 亚马逊上传提要,将产品与现有产品匹配?

c# - 将当前项目作为数据模板中的命令参数发送

c# - TableLayoutPanel GetControlFromPosition 不会获取不可见的控件。如何访问指定位置的不可见控件?

jquery - Gmap 3-如何使用Ajax Json请求标记+有点击事件?

javascript - 如何确定 IE 中动态加载图像的 img 宽度/高度?

c# - mvvm一般的疑惑和messageBox

jquery - 将密码字段中的默认值显示为明文

c# - 将代码中的 userid 和 guid 提供给 signalr 服务器

jquery - 为什么我的 jQuery 验证需要点击 2 次才能调用?

c# - ASP.NET WebAPI 2 嵌套 JSON