ASP.NET – 使用jQuery基于JSON的Web服务的正确方法是什么?

将基于ASP.NET SOAP的Web服务转换为基于JSON的响应的正确方法是什么? …然后从jQuery调用这些?

在集成基于jQuery的AJAX和ASP.NET时,什么是“最佳实践”? 文章? 图书?

可以使用System.Runtime.SerializationSystem.Runtime.Serialization.JSON将JSON转换为.NET类。 我怀疑你对设置从客户端到服务器的函数调用更感兴趣。 我认为值得尝试本教程 。

在本教程中,您需要添加一个webservice’.asmx’文件。 在asmx文件中,您将能够创建可从客户端脚本调用的函数。 您的ASP.NET页面还可以引用生成的客户端脚本以调用.asmx函数。

如果您确实想要进行JSON序列化,您还可以使用以下内容:

using System.Runtime.Serialization; using System.Runtime.Serialization.Json; public class JsonSerializer { // To make a type serializeable, mark it with DataContractAttribute // To make a member of such types serializeable, mark them with DataMemberAttribute // All types marked for serialization then need to be passed to JsonSerialize as // parameter 'types' static public string JsonSerialize(object objectToSerialize, params Type[] types) { DataContractJsonSerializer serializer = new DataContractJsonSerializer( types[0], types.Skip(1)); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms, objectToSerialize); ms.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(ms); return sr.ReadToEnd(); } } 

以下文章在codproject.com上扩展现有的ASP.NET Web服务以支持 Bobby Soares的JSON ,讨论如何使用自定义方法属性来实现所需的结果。

我已经使用了ASP.Net Ajax一段时间了,但没有担心JSON或XML通信。 相反,我使用Web服务直接返回您可以使用innerHTML设置的内容。

这很容易实现。 只需创建一个Web服务(ASMX)文件,并将您的方法声明为WebMethods(设置WebMethod属性)。

现在,您可以使用Javascript代码调用Web服务,就像常规function一样。

函数的结果将返回到回调函数。 这是结构

 //Webmethod returns some HTML content Myservice.DoSomething(myParam, callBackFunction); //Content is set on the webpage function callBackFunction(result){ document.getElementById('myElemID').innerHTML = result; }