locked
Json webservice RRS feed

  • Question

  • User593747808 posted

    Dear all,

    I want to develop a webservice that returns a json like the following:

    http://niceq8i.tv/mobile/api/cat/get_sounds

    How to make the functions has a direct link like that and return the json code without any xml code

    Thanks in advance...

    Wednesday, December 25, 2013 3:11 AM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 25, 2013 9:39 PM
  • User-417640953 posted

    Hi brightline,

    Thanks for the post.

    According to your description and link provided, I see that you want the webservice return json format data to client side.

    I made a simple demo below, please check it.

    Webservice:

        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public List<News> GetNewsList()
            {
                List<News> list = new List<News>() { 
                new News{ Id=1, Title="hello world!"},
                new News{ Id=2, Title="Happy Xmas!"}
                };
                return list;
            }
        }
    
        public class News {
    
            public int Id { get; set; }
            public string Title { get; set; }
    
        }

    We can call it from client side using javascript.

    <head runat="server">
        <title></title>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script>
            $(function () {
    
                $("#Button1").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "WebService1.asmx/GetNewsList",
                        data: {},
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                            
                            $("#result").html(JSON.stringify(msg.d));
                           // alert(JSON.stringify(msg.d));
                        },
                        error: function (e) {
                            alert(e);
                        }
                    }); 
                });
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <div id="result"></div>
            <input id="Button1" type="button" value="button" />
        </div>
        </form>
    </body>

    Thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 26, 2013 9:49 AM

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 25, 2013 9:39 PM
  • User-417640953 posted

    Hi brightline,

    Thanks for the post.

    According to your description and link provided, I see that you want the webservice return json format data to client side.

    I made a simple demo below, please check it.

    Webservice:

        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public List<News> GetNewsList()
            {
                List<News> list = new List<News>() { 
                new News{ Id=1, Title="hello world!"},
                new News{ Id=2, Title="Happy Xmas!"}
                };
                return list;
            }
        }
    
        public class News {
    
            public int Id { get; set; }
            public string Title { get; set; }
    
        }

    We can call it from client side using javascript.

    <head runat="server">
        <title></title>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script>
            $(function () {
    
                $("#Button1").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "WebService1.asmx/GetNewsList",
                        data: {},
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                            
                            $("#result").html(JSON.stringify(msg.d));
                           // alert(JSON.stringify(msg.d));
                        },
                        error: function (e) {
                            alert(e);
                        }
                    }); 
                });
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <div id="result"></div>
            <input id="Button1" type="button" value="button" />
        </div>
        </form>
    </body>

    Thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 26, 2013 9:49 AM