简体   繁体   English

如何在C#中反序列化在JQuery中序列化的内容?

[英]How to deserialize in C# something that was serialized in JQuery?

. .

It's been a while since I've done this, and I'm trying to shake off the rust. 自从我这样做已经有一段时间了,我正在努力摆脱铁锈。

I'm trying to set up an AJAX structure in ASP.NET using VS2010. 我正在尝试使用VS2010在ASP.NET中建立AJAX结构。

I have a JQuery form submit that looks something like this (greatly simplified for example purposes): 我有一个看起来像这样的JQuery表单提交(出于示例目的,已大大简化):

function FormSubmit () {
    $.post('SomeHandler.asmx/SomeFunction', 
        $("#form1").serialize(), 
        function(data) {some data handler}
    );
}

My "SomeHandler.asmx/SomeFunction" is a C# function that takes my form data and processes it. 我的“ SomeHandler.asmx / SomeFunction”是一个C#函数,它接收我的表单数据并对其进行处理。

To my knowledge, the SomeHandler.asmx assumes XML deserialization, but the JQuery serializes it as an HTML encoded string, not as XML. 据我所知,SomeHandler.asmx假定XML反序列化,但是JQuery将其序列化为HTML编码的字符串,而不是XML。

I suppose to use an analogy, one side is speaking in English, but the other side is expecting to hear French. 我想打个比方,一侧是英语,但另一侧则是法语。

How do I get around this? 我该如何解决? (For example purposes, let's say my form has a text field -- we'll call it "txtField", and a drop-down list -- let's call it "lstDropDown".) (出于示例目的,假设我的表单具有一个文本字段-我们将其称为“ txtField”,以及一个下拉列表-将其称为“ lstDropDown”。)

Thanks in advance for your help! 在此先感谢您的帮助!

In your asmx file make sure you use the following attribute flags on your web methods that accept and respond with json: 在您的asmx文件中,请确保您在接受和响应json的网络方法上使用以下属性标志:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string SomeWebMethod() 
{
  //blah
}

Also, make sure the web service class has the follow attribute flag: 另外,请确保Web服务类具有follow属性标志:

[System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService
{
...
}

One last thing: if you're serializing the data on the client side, you need to use the following ajax setup: 最后一件事:如果要在客户端上序列化数据,则需要使用以下ajax设置:

 $.ajaxSetup({ contentType: "application/json; charset=utf-8" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM