consume .net web service using jquery

后端 未结 4 1428
猫巷女王i
猫巷女王i 2021-01-03 13:34

I made this web service that returns a datatable from sql server db. Can someone help me with the jquery to display it?

web service

[WebService(Name         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 14:05

    In the past, when using asmx services with jQuery, I used something like the following for post/json:

    Assuming that I had a response class like this:

        public ResponseClass
        {
            public string Message { get; set; }
        }
    

    And a webservice with a method like this:

        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public ResponseClass PostResponse()
        {
            var response = new ResponseClass() {Message = "Hello World"};
            return response;
        }
    

    Some html like this:

    The javascript:

    $.ajax({
        url: '/MyService.asmx/PostResponse',
        data: "{}",
        type: "POST",
        cache: false,
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            var response = msg.d; //your response object
            $('#response').html(response.Message); //set the response div contents to the Message 
        },
        error: function(xhr, status, error) {
            alert(error); //do something if there is an error
        }
    });
    

提交回复
热议问题