jQuery的$。阿贾克斯()调用的WebMethod [英] Jquery $.ajax() call to webmethod

查看:154
本文介绍了jQuery的$。阿贾克斯()调用的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有使用过的$。阿贾克斯(),如果你看到任何输入错误,让我知道;)

I have never before used the $.ajax() and if you see any mistypes let me know ;)

即时通讯使用jQuery $。阿贾克斯()调用使用JSON一个WebMethod。

Im using jQuery $.ajax() to call a webmethod with JSON.

将WebMethod的简单定义应该是这个样子:

The simple definition of the webmethod should look something like this:

[WebMethod]
public static bool MyMethod(string a, string b, string c) {
   ...
}

在$阿贾克斯(数据参数)的值是:

The value for the data parameter in $.ajax() is:

的myData =>'一':'val_a','B':'val_b','C':'val_c

myData => "'a':'val_a', 'b':'val_b','c':'val_c'"

下面是我的ajax调用:

Here is my ajax call:

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

现在的棘手部分。我需要一个额外的参数添加到我的webmethod。我必须向我的webmethod所有复选框一定的DIV中,他们的名字,如果他们进行检查。我有jQuery的code选择这些值。这里是我正常看code-后面编程的附加参数会像

Now the tricky part comes. I need to add an extra parameter to my webmethod. I must send to my webmethod all checkboxes inside a certain div, their names and if they are checked. I have Jquery code to select these values. Here is where i see in normal code-behind programming an additional parameter would be like

[WebMethod]
public static bool MyMethod(string a, string b, string c, Dictionary<string,bool> dict) {
       ...
}

保存我的复选框的文本值,如果它的检查。

that holds my checkbox text-value and if it's checked.

我没有foggiest想法JSON是如何工作,我所知道的是,我必须使这项工作很快。

I haven't the foggiest idea how JSON actually works, all i know is that i have to make this work soon.

可能某种多维Javascript角阵列必须使用。如果您对这个问题的最佳办法的任何想法,我会很高兴!

Probably some sort of multi dimensional javascripts arrays must be used. If you have any ideas on the best approach for this question i would be glad!

/丹尼尔·斯文森,瑞典

/Daniel Svensson, Sweden

推荐答案

如果你让你的bool空,你可以使用.serialize()方法,在所有你的价值观,而不是在发布数据的复选框将简单地通过该方法被忽略:

if you make your bools nullable, you can just use the .serialize() method on all your values, and the checkboxes that aren't in the posted data will simply be ignored by the method:

[WebMethod]
public static bool MyMethod(string a, string b, string c, bool? chkbox1, bool? chkbox2....) {
       ...
}

您需要添加一个布尔?参数页面上的所有可能的复选框。这是为测试等提供了很好的方法,但是如果你真的想你可以添加另一个目的是你的数据,最好的办法,例如

you would need to add a bool? parameter for all your possible checkboxes on the page. that's the best way to provide a good method for testing etc. however if you really wanted to you could add another object to your data, for instance

data: { 'a':'val_a', 'b':'val_b','c':'val_c', 'dict': { 'chk1', 'chk2', 'chk3' } }

在这里CHK1,CHK2,chk3是你已经建立了检查复选框的数组,和你的字典参数只会成为一个字符串数组。

where chk1, chk2, chk3 is an array you've built of checked checkboxes, and your dict param would simply become a string array.

这篇关于jQuery的$。阿贾克斯()调用的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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