How to call c# method with no parameters and access returned data?

后端 未结 2 1145
醉梦人生
醉梦人生 2021-01-22 16:08

So I have seen many examples such as these : https://stackoverflow.com/a/8094230/2525507

public class WebService : System.Web.Services.WebService {
   [WebMetho         


        
相关标签:
2条回答
  • 2021-01-22 16:15

    If your method takes no arguments, just don't specify the data property on the ajax call

    <script>
      $(function () {
        $.ajax({
          url: 'WebService.asmx/getList',
          type: 'POST',
          dataType: 'json', //make sure your service is actually returning json here
          contentType: 'application/json',
          success: function (data, status) {
            //here data is whatever your WebService.asmx/getList returned
            //populate your dropdown here with your $.each w/e
          }
        });
      });
    </script>
    

    Also I could be wrong, but the WebService method you showed, doesn't look like it will return json. I think you will have to serialize, or set the content type or something similar. (Been along time since I've used the asmx type services)

    0 讨论(0)
  • 2021-01-22 16:40

    See my answer to this post. I reference a web site called Encosia, written by Dave Ward. He has an excellent series on using Ajax with ASP.net / MVC. That is a great place to start, with numerous examples.

    0 讨论(0)
提交回复
热议问题