如何循环遍历从 JQuery $.ajax 方法发送到 ASP.NET WebMethod 的 POST 变量?
我有一个使用 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 种方法:
- 页面方法(带有
[WebMethod]
修饰符的 .aspx 页面中的静态方法) 中方法之间的 Switch 语句.aspx 页面的 Page_Load
- 在 .ashx 页面
- 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:
- Page Method (static method in a .aspx page with the
[WebMethod]
modifier) - Switch statement between methods in
Page_Load
of a .aspx page - Switch statement between methods in
ProcessRequest
of a .ashx page - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Selections 变量应该是一个对象而不是字符串。
The selections variable should be an object not a string.
首先,确保您的选择变量是有效的 JSON,您在问题中编写的内容不是有效的 JSON 符号。
其次,在您的 webmethod 定义中,让 MyMethod 接收一个字符串作为参数;假设您将其声明为
在方法正文中,使用 JavascriptSerializer 反序列化
Dictionary
中的字符串,例如:现在您可以循环遍历字典中的元素。
澄清一下,您的选择变量需要将每个属性都用引号引起来;在您的情况下,id1、id2...需要为 'id1':'comment'、'id2':'comment' 等。
编辑
添加代码来演示如何调用 Web 服务。
最后的建议是,熟悉 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
In the body of your method., deserialize the string in a
Dictionary<string,string>
using the JavascriptSerializer, for example: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.
As a last advice, get familiar with jsFiddle, it allows you to test things quickly. See this, for example.