Ajax call.net WCF

Posted by Wolphie on Tue, 27 Aug 2019 18:14:38 +0200

The experiment has been repeated many times, but it is actually simpler, but we have taken a lot of detours. To sum up now,
Environment: VS2015

  1. Creating.net WCF is easy. I use IIS as service support to add new WCF directly to my web application project.

2. Add a service interface (which I'm used to) that defines several methods that need to be published.

    interface ISystemUserSvr
    {
        [OperationContract]
        [WebGet]
        List<SystemUser> RetrieveUser();

        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        List<SystemUser> RetrieveBy(string loginId, string userName);

        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        List<SystemUser> RetrieveByObj(SystemUser user);

    }

The first method uses Get, and the other two receive parameters using Post.Post is the default, and there are no settings here, including Request and Response settings.

  1. Method implementation is simple, write directly.Here's just an example

        public List<SystemUser> RetrieveUser()
        {
            return (new SystemUserBO()).Retrieve(); 
        }
    
        public List<SystemUser> RetrieveBy(string loginId, string userName)
        {
    
            return (new SystemUserBO()).Retrieve(loginId, userName, 0, 0);
        }
    
        public List<SystemUser> RetrieveByObj(SystemUser user)
        {
            return (new SystemUserBO()).Retrieve(user.LoginId, user.UserName, 0, 0);
        }
  2. The web.config configuration does not require special configuration at all. Use the MS Configuration Editor (VS comes with it), pay attention to the ReaderQuotas settings, and keep the maximum length as long as possible.

  3. Client invocation, list several invocation methods

    • Get mode, no parameters

          $.ajax({
              contentType: 'application/json;charset=utf-8',
              method: "get",
              type: "get",
              dataType: "json",
              url: "SystemUserSvr.svc/Retrieve",
              success: function (result) {
                  $.each(result.d, function (n, value) {
      
                      $('#ajaxResult').append(value.UserName + "<p>" + n);
                  }
                     );
              }
          });
      
      });
  • Post mode, passing Json string parameters

            $.ajax({
                contentType: 'application/json;charset=utf-8',
                method: "post",
                type: "post",
                data: JSON.stringify({ "LoginId": "R", "UserName": "" }),
                dataType: "json",
                url: "SystemUserSvr.svc/RetrieveBy",
                success: function (result) {
                    $.each(result.d, function (n, value) {
    
                        $('#ajaxResult').append(value.UserName + "<p>" + n);
                    }
                       );
                }
            });
    
        });
  • Post mode, passing Json object

            $.ajax({
                contentType: 'application/json;charset=utf-8',
                method: "post",
                type: "post",
                data: JSON.stringify({ "user": { "LoginId": "R", "UserName": "king" } }),
                dataType: "json",
                url: "SystemUserSvr.svc/RetrieveByObj",
                success: function (result) {
                    $.each(result.d, function (n, value) {
    
                        $('#ajaxResult').append(value.UserName + "<p>" + n);
                    }
                       );
                }
            });

The most important thing to note about a Post parameter is the setting of that contentType, contentType:'application/json; charset=utf-8'cannot be omitted; otherwise, the parameter may not succeed.In addition, the data returned by.net WCF may be a D object, so it is important to note that when parsing the returned data, the example here uses result.d directly to get the value of the D object.

Topics: Web Development JSON IIS