Returning JSON from ASMX, and handling it correctly in Javascript

后端 未结 5 1693
轮回少年
轮回少年 2020-12-18 02:07

I realize there are tonnes of similar questions already up here but I cannot figure this one out.

I have a Web Service (C#, .net 3.5). The essential Code you need

5条回答
  •  佛祖请我去吃肉
    2020-12-18 03:00

    i have bad english, but this is a solution

    ----- WSMember.asmx ------

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class WSMember : System.Web.Services.WebService {
    
         public WSMember () {   
         }
    
         [WebMethod]
         [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
         public string GetMember(int IdMember)
         {
             Member m = new Member();//Get Member from DB, exam Linq to Sql
             m.Surname = "Smith";
             m.FirstName = "John";
             return string.Format("{{ \"Surname\":\"{0}\",\"FirstName\":\"{1}\" }}",m.Surname,m.FirstName);
         }
    }
    

    ---- Default.aspx ----

    Just a test
    
    

提交回复
热议问题