如何循环遍历从 JQuery $.ajax 方法发送到 ASP.NET WebMethod 的 POST 变量?

发布于 2024-12-02 16:59:10 字数 1074 浏览 1 评论 0原文

我有一个使用 HTML 按钮创建的清单,单击时可以在“选定”和“未选定”类之间切换。每个选项都有一个与其相关的 ID 以及一个可选的注释输入。

我正在尝试将 JQuery $.ajax() 方法与 POST 结合使用来发送所选按钮的 ID 和相关评论。我的想法是在单击“保存”时创建所选 ID 及其各自注释的键/值集合,如下所示:

selections = "{id1: 'comment1', id2: 'comment2', ...}"

然后将其传递到 $.ajax() 方法的数据部分,如下所示:

$.ajax({
     type: "POST",
     url: "MyPage.aspx/MyMethod"
     data: selections,
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(msg) {
       onSuccess(msg);
     }
});

最后访问通过循环访问 HttpContext.Current.Request.Form 来选择 MyMethod 中的内容。

但是当我尝试这个时,HttpContext.Current.Request.Form 始终为空。我的做法有错吗?如果是这样,为什么?谁能建议另一种方法?

任何帮助将不胜感激。提前致谢!

另外,我知道从 JQuery AJAX API 调用服务器方法的 4 种方法:

  1. 页面方法(带有 [WebMethod] 修饰符的 .aspx 页面中的静态方法)
  2. 中方法之间的 Switch 语句.aspx 页面的 Page_Load
  3. 在 .ashx 页面
  4. Web 服务

ProcessRequest 中的方法之间切换语句哪种方法最有效?我认为 2 会涉及一个不必要的页面实例,因此不应该是这个。

I have a checklist created with HTML buttons that toggle between 'selected' and 'unselected' classes on click. Each option has an ID related with it and also an optional comment input.

I am trying to use the JQuery $.ajax() method with POST to send the IDs of the selected buttons and the related comment. The idea I have is to create a key/value collection of the IDs selected and their respective Comments when 'Save' is clicked like so:

selections = "{id1: 'comment1', id2: 'comment2', ...}"

Then pass it to the data portion of the $.ajax() method like so:

$.ajax({
     type: "POST",
     url: "MyPage.aspx/MyMethod"
     data: selections,
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(msg) {
       onSuccess(msg);
     }
});

And finally access the selections in MyMethod by looping through HttpContext.Current.Request.Form.

But when I try this, HttpContext.Current.Request.Form is always empty. Is my approach wrong? If so, why? Can anyone suggest another approach?

Any help would be greatly appreciated. Thanks in advance!

Also, I know of 4 ways to call a server method from the JQuery AJAX API:

  1. Page Method (static method in a .aspx page with the [WebMethod] modifier)
  2. Switch statement between methods in Page_Load of a .aspx page
  3. Switch statement between methods in ProcessRequest of a .ashx page
  4. Web Service

Which way is most efficient? I assume 2 would involve an unnecessary instance of a Page, so that shouldn't be the one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皓月长歌 2024-12-09 16:59:10

Selections 变量应该是一个对象而不是字符串。

var selections = { id1: 'a', id2: 'b' };

The selections variable should be an object not a string.

var selections = { id1: 'a', id2: 'b' };
野鹿林 2024-12-09 16:59:10

首先,确保您的选择变量是有效的 JSON,您在问题中编写的内容不是有效的 JSON 符号。

其次,在您的 webmethod 定义中,让 MyMethod 接收一个字符串作为参数;假设您将其声明为

[WebMethod]
public MyMethod(string selections)
{
    //Do processing here
}

在方法正文中,使用 JavascriptSerializer 反序列化 Dictionary 中的字符串,例如:

JavascriptSerializer js = new JavascriptSerializer(); 
Dictionary<string,string> result = js.Deserialize<Dictionary<string,string>>(selections);

现在您可以循环遍历字典中的元素。

澄清一下,您的选择变量需要将每个属性都用引号引起来;在您的情况下,id1、id2...需要为 'id1':'comment'、'id2':'comment' 等。

编辑

添加代码来演示如何调用 Web 服务。

var selections = "{'id1': 'comment1', 'id2': 'comment2'}";

$.ajax({
     type: "POST",
     url: "MyPage.aspx/MyMethod"
     data: JSON.stringify(selections),
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(msg) {
       onSuccess(msg);
     }
});

最后的建议是,熟悉 jsFiddle,它可以让你快速测试。例如,请参阅此

First, make sure your selections variable is valid JSON, what you wrote in your question is not valid JSON notation.

Second, in your webmethod definition, have the MyMethod receive a string as parameter; lets say you declare it as

[WebMethod]
public MyMethod(string selections)
{
    //Do processing here
}

In the body of your method., deserialize the string in a Dictionary<string,string> using the JavascriptSerializer, for example:

JavascriptSerializer js = new JavascriptSerializer(); 
Dictionary<string,string> result = js.Deserialize<Dictionary<string,string>>(selections);

Now you can loop through the elements in your dictionary.

Just to clarify, your selections variable needs to have every property enclosed in quotes; in your case, id1, id2... need to be 'id1':'comment','id2':'comment', etc.

EDIT

Adding code to demonstrate how the web service should be called.

var selections = "{'id1': 'comment1', 'id2': 'comment2'}";

$.ajax({
     type: "POST",
     url: "MyPage.aspx/MyMethod"
     data: JSON.stringify(selections),
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(msg) {
       onSuccess(msg);
     }
});

As a last advice, get familiar with jsFiddle, it allows you to test things quickly. See this, for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文