使用jquery将密钥值对作为JSON发送到ASMX服务

我一直在研究联系表格,我是ASP.net的新手,我知道中间数量的C#我在联系表格上工作。 我想将值作为json数组发送并使用JSON.net进行解析,我尝试了各种方法来让它工作。 如果没有成功,我需要知道如何从ASMX页面正确发送和接收JSON。 是否有示例文件或教程? 或者可以请一些人告诉我我做错了什么? 这是我能够阅读后变量的唯一方法。

但它只有2个数组而不是键值对。

       
$(document).ready(function () { var $serialize = $('form').serializeArray(); var stringify = JSON.stringify($serialize); var keys = ['firstname', 'lastname']; var list = [$('#firstname').val(), $('#lastname').val()]; var jsonText = JSON.stringify({ args: list, keys: keys }); $.ajax({ url: "validation.asmx/sendRequest", method: "POST", dataType: "json", data:jsonText, cache: false, processData: true, contentType: "application/json; charset=utf-8" }).done(function (data) { console.log(data); }); });

这是我的asmx文件,

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using Newtonsoft.Json; using System.Web.Script.Services; using System.Data; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class validation : System.Web.Services.WebService { public validation () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string sendRequest(List args, List keys) { var arg = args; var key = keys; return args[0]; } } 

这是我的网络配置文件

              

我可以回答你。 以上代码似乎是您尝试解决问题。

要传递一个字符串数组,请参阅下面给出的javascript代码

  var MyStringArray = new Array(); MyStringArray.push("firstval"); MyStringArray.push("secondval"); var StringifiedContent = JSON.stringify('varName':MyStringArray); $.ajax({ type: "POST", url: "validation.asmx/sendRequest",//Path to webservice data: StringifiedContent, contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { } }); 

您可以在webService中接受它,如下所示

 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string sendRequest(List varName)//The variable name must be same { foreach(var eachvals in varName) { } } 

如果您按照上面显示的代码进行操作,则无需担心JSON格式。 如果您不打算使用类似操作的服务,那么在页面后面使用[WebMethod]将是更好的选择。

您可以传递用户定义的javaScript对象,而不是传递字符串。 在这种情况下,它会,

 var MyStringArray = new Array(); var MyObject = {}; MyObject.Key="123"; MyObject.Value="abc"; MyStringArray.push(MyObject); var StringifiedContent = JSON.stringify('varName':MyStringArray); $.ajax({ type: "POST", url: "validation.asmx/sendRequest",//Path to webservice data: StringifiedContent, contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { } }); 

然后,您可以在webService中接受它,如下所示

 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string sendRequest(List varName)//The variable name must be same { foreach(var eachvals in varName) { string Keyval =eachvals.Key; string Value =eachvals.Value; } } public class MyObject { public string Key {get;set}; public string Value {get;set;} } 

或者,如果您不想为每个使用的方法创建类,则可以使用词典。

 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string sendRequest(Dictionary varName)//The variable name must be same { foreach(var eachvals in varName) { string Keyval =eachvals.["Key"]; string Value =eachvals.["Value"]; } } 

您可以将其作为字符串获取并将其反序列化为对象。 发送你的“jsonText”

我在我的项目中这样做。 我只是将FromJSON放在一个类中并将其用作扩展*你不必做类的事情

示例:“{args:[‘aaa’,’bbb’,’ccc’,’ddd’],键:[‘aaa’,’bbb’,’ccc’,’ddd’]}”

*使用System.Web.Script.Serialization;

 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string sendRequest(string msg) { MyClass mc = FromJSON(msg) return mc.args[0]; } class MyClass { internal string[] keys; internal string[] args; } T FromJSON(string json) { JavaScriptSerializer serializer = new JavaScriptSerializer(); T o = serializer.Deserialize(json); return o; } 

.NET Framework的标准KevValuePair类不起作用,因为setter是私有的。

我已经在你的代码中看到你不是新手,所以只需创建一个带有公共getter / setter的小class,你就可以了。

 [Serializable] public class MyKVP { public string k { get; set; } public string v { get; set; } } 

现在,在您的Web方法中,您的签名应更改为:

 public string sendRequest(MyKVP[] args); 

您的JavaScript代码将需要此更改:

 var kvps = [ {k:'firstname', v: $('#firstname').val()}, {k:'lastname', v: $('#lastname').val()} ]; var jsonText = JSON.stringify({ args: kvps });