ASP.NET What is the right approach to JSON web services using jQuery?

What is the correct way to convert ASP.NET SOAP-based web services to JSON-based responses? ... and then call them from jQuery?

What are the “best practices” for integrating jQuery based on AJAX and ASP.NET? Articles? Books?

+5
source share
3 answers

Converting JSON to .NET classes can be done using System.Runtime.Serialization and System.Runtime.Serialization.JSON . I suspect that you are more interested in setting up function calls from client to server. I think it's worth trying this tutorial .

In this tutorial, you need to add a .asmx file for webservice. In the asmx file, you can create functions called by the script client. ASP.NET pages can also reference a client script generated to call .asmx functions.

If you really want to serialize JSON, you can also use the following:

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();
    }
}
+3
source

- ASP.NET JSON Bobby Soares codproject.com .

+3

ASP.Net Ajax , JSON XML-. - , innerHTML.

. - (ASMX) WebMethods ( WebMethod).

- Javascript- , .

.

//Webmethod returns some HTML content
Myservice.DoSomething(myParam, callBackFunction);

//Content is set on the webpage
function callBackFunction(result){
  document.getElementById('myElemID').innerHTML = result;
}
0

All Articles