How to get JSON response from a 3.5 asmx web service

后端 未结 4 1410
梦谈多话
梦谈多话 2020-11-27 07:03

I have the following method:

using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using         


        
4条回答
  •  醉酒成梦
    2020-11-27 07:20

    Dear future readers: The currently accepted answer is not the right way. It ties you to using the JavaScriptSerializer and you lose the ability to request xml (or indeed any serialization format which may come along in the future). The "right way" also involves less code!

    If you decorate your service class with the [ScriptService] attribute - which you have - then ASP.NET 3.5+ should automatically serialise the response to JSON provided your Ajax call requests JSON. The suggestions to serialise to JSON manually are simply wrong, unless you wish to use a different serialiser such as Newtonsoft.

    That you were seeing XML suggests one of the following:

    • You are not requesting JSON in your Ajax call - please see working example code below
    • Possibly some web.config entries are missing, according to an answer here (disclaimer: I don't have most of these in a production web.config; only start playing with these if nothing else works)

    Here is a simple working example of a JSON enabled ASMX web service:

    <%@ WebService Language="C#" Class="WebService" %>
    using System;
    using System.Collections.Generic;
    using System.Web.Services;
    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService {
        [WebMethod]
        public MyClass Example()
        {
            return new MyClass();
        }
    
        public class MyClass
        {
            public string Message { get { return "Hi"; } }
            public int Number { get { return 123; } }
            public List List { get { return new List { "Item1", "Item2", "Item3" }; } }
        }
    }
    

    JavaScript to request it and process the response (we'll simply pop up a JS alert with the message from MyClass.Message) :

    
    
        Test
          
    
    
        
    
    
    

    Http request:

    POST http://HOST.com/WebService.asmx/Example HTTP/1.1
    Accept: application/json, text/javascript, */*; q=0.01
    Content-Type: application/json; charset=utf-8
    X-Requested-With: XMLHttpRequest
    Referer: http://HOST.com/Test.aspx
    Accept-Language: en-GB,en;q=0.5
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
    Connection: Keep-Alive
    Content-Length: 3
    Host: HOST.com
    
    { }
    

    HTTP response:

    HTTP/1.1 200 OK
    Cache-Control: private, max-age=0
    Content-Type: application/json; charset=utf-8
    Server: Microsoft-IIS/8.0
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Date: Tue, 20 Feb 2018 08:36:12 GMT
    Content-Length: 98
    
    {"d":{"__type":"WebService+MyClass","Message":"Hi","Number":123,"List":["Item1","Item2","Item3"]}}
    

    Result:

    "Hi" is displayed in a JS popup.

提交回复
热议问题