Wednesday, 20 November 2013

JSON With Web Service AJAX Call In c# Asp.NET

here i have created on service and called that service with use of ajax function from javascript.

the service is returning list as json

data to ajax function as a result.

(i have just explain you a basic example, you can return what ever you want to return)


WebService1.asmx.cs File

This file is having service method

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Web.Script.Services;
using System.Runtime.Serialization;
using Newtonsoft.Json;     ///Add the reference of JSON.NET Dll for the json converter used in program

namespace TestJavascriptServices
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    public class WebService1 : System.Web.Services.WebService

        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public String HelloWorld()

            Hashtable ht = new Hashtable();
            ht.Add("me", "jayesh");
            ht.Add("you", "CPU");
            ht.Add("My", "Mind");
            ht.Add("Your", "Processor");
            ht.Add("And", "Blast");

            List<String> ls = new List<string>();
            Test t = new Test();

            t.setData("me", "jayesh");
            //return "{\"me\":\"Hello this is jayesh\",\"u\":\"hsdh\",\"t\":\"dhdsfh\",\"s\":\"dsfhsdjh\"}";
            return JsonConvert.SerializeObject(t.getData());
            //return JsonConvert.SerializeObject(ls);
            //return JsonConvert.SerializeObject(ht);
            //return JsonConvert.SerializeObject(t);


    public class Test {
        public string Name;
        public string salary;

        public void setData(string n,string s)
            this.Name = n;
            this.salary = s;

        public Test getData() {
            return this; 



This file is having ajax functions which will call service 

var test;
function pageLoad() 
    test = new TestJavascriptServices.WebService1();

function SucceededCallback(result) {
    var RsltElem = document.getElementById("Results");

    var dd = $.parseJSON(result);
    RsltElem.innerHTML =;

function calll() {
    var greetings = test.HelloWorld();


function FailedCallback(error, userContext, methodName) {
    if (error !== null) {
        var RsltElem = document.getElementById("Results");

        RsltElem.innerHTML = "An error occurred: " +

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();


This file is having html inputs


<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="TestJavascriptServices._Default" %>

<script language="javascript" type="text/javascript" src="" ></script>
<asp:ScriptManager runat="server" ID="scriptManager">
                    <asp:ServiceReference path="~/WebService1.asmx" />
                    <asp:ScriptReference Path="~/TestWebServiceHello.js" />

        Welcome to ASP.NET!
        To learn more about ASP.NET visit <a href="" title="ASP.NET Website"></a>.
        You can also find <a href=";clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    <span id="Results"></span>

    <input type="button" id="btn" onclick="calll();return false;" value="Click me" />

Newtonsoft plugin from here and add the reference to the project file