Click here to Skip to main content
15,881,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, good morning,

how to create JSON webservice in asp.net.

normally before, we used to create webservice with extension .asmx.

so, is JSON webservice created with wcf service or .asmx service. how to create JSON webservice.

please explain with an example.

thank you.
Posted
Comments
Prasad N Joshi 23-Jul-13 3:39am    
you need datatable output from service as Json format?

This article might be useful for you: A Beginner's Tutorial on Creating WCF REST Services[^]
 
Share this answer
 
Try This Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.Serialization.Json;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
    public WebService()
    {  }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public CityList GetCities()
    {
        CityList oBoCityList = new CityList() { new City { Name = "New Delhi", ID = 1 }, new City { Name = "Kanpur", ID = 2 }, new City { Name = "Gurgaon", ID = 3 } };
        return oBoCityList;
    }
}

public class City
{
    public City() { }
    public string Name
    { get; set; }
    public Int32 ID
    { get; set; }
}

public class CityList : List<City>
{
    public CityList() { }
}



XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" src="Script/jquery-1.7.2.min.js"></script>

    <script language="javascript" type="text/javascript">
        $.ajax({
            type: 'POST',
            url: 'WebService.asmx/GetCities',
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function(jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            },
            success: function(data) {
                var cities = data.d;
                $.each(cities, function(index, cities) {
                    alert("City Name: " + cities.Name + "\nID: " + cities.ID);
                });
            }
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>
 
Share this answer
 
Comments
GuyThiebaut 27-Jul-14 8:44am    
Thanks buddy!
I have spent most of the weekend trying to get a webservice and calling script working - trawling hundreds of pages and your solution works.

If you're ever in Cambridge UK I will buy you a beer!
Take a look here[^]; or if you want to [try|could use] something different, try NancyFX[^]

Rough on the first steps but totally worth it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900