ASP.NET JSON web service always return the JSON response wrapped in XML

后端 未结 9 1369
清歌不尽
清歌不尽 2020-12-05 20:17

I saw a similar question but it did not resolve my issue. I have a JSON web service in an ASMX file;

The code for the web method

        [WebMethod]         


        
9条回答
  •  盖世英雄少女心
    2020-12-05 20:48

    Your missed only "static" keyword in method declaration please look into the following it should work.

    FYI: All the web methods should be static

    [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

        public static string GetUserRoles(string JSONUserCode)
        {
            string retRoles = string.Empty;
            List roles = new List();
    
            {... I Populate the roles here ...}
    
            DataContractJsonSerializer serializer = new
            DataContractJsonSerializer(roles.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, roles);
            string jsonString = Encoding.Default.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }
    

提交回复
热议问题