none
How to pass a stringified object from AJAX Post and receive it as class type in WCF service? RRS feed

  • Question

  • I have created a method in my IService as:


     [OperationContract] 

    [WebInvoke(UriTemplate="LogInForMobileWithDeviceNo",Method="POST",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]  string LogInForMobileWithDeviceNo(clsUserDeviceInfo userDeviceInfo);

    In the LogInForMobileWithDeviceNo method, the clsUserDeviceInfo is a class.

     [DataContract]
        public class clsUserDeviceInfo
        {
            [DataMember]
            public string UserID{get;set;}
            [DataMember]
            public string DeviceName{get;set;}
            [DataMember]
            public string CordovaVersion{get;set;}
            [DataMember]
            public string DevicePlatformJs{get;set;}
            [DataMember]
            public string DeviceUID{get;set;}
            [DataMember]
            public string DeviceModel { get; set; }
            [DataMember]
            public string DeviceVersion { get; set; }
        }

    When I call this method using AJAX Post, it is not working. I can't find what is going wrong?

    Using Jquery I am doing Ajax Post:

        var serverurl = "http://localhost:51911/Service1.svc/LogInForMobileWithDeviceNo";
            var DeviceName = "samsung";
            var CordovaVersion = "2.1.1.1";
            var DevicePlatformJs = "windows 8";
            var DeviceUID = "23dswd-234dff-23-2334nhj";
            var DeviceModel = "grand duos";
            var DeviceVersion = "3.2";
             var DataArr = {DeviceName:DeviceName,CordovaVersion:CordovaVersion, DevicePlatformJs:DevicePlatformJs,DeviceUID:DeviceUID,DeviceModel:DeviceModel,DeviceVersion:DeviceVersion};
             $.ajax({
                type: "GET",               
                url: serverurl,
            data: JSON.stringify(DataArr),
                success: function (result) {
                    alert(result);
                },
                accept: 'application/json'
            });

    But it's not working.

    Kindly help me on the above issue.

    Thanks in advance. :)

    Wednesday, September 25, 2013 10:36 AM

Answers

  • Hi,

    Please try to check if you have changed the default configuration created by Visual Studio in Web.Config file for WCF services, so that it works with the HTTP protocol request send by jQuery client code.

    <system.serviceModel>
      <behaviors>
       <serviceBehaviors>
        <behavior name="ServiceBehavior">
         <serviceMetadata httpGetEnabled="true"/>
         <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
       </serviceBehaviors>
       <endpointBehaviors>
        <behavior name="EndpBehavior">
         <webHttp/>
        </behavior>
       </endpointBehaviors>
      </behaviors>
      <services>
       <service behaviorConfiguration="ServiceBehavior" name="Service1">
        <endpoint address="" binding="webHttpBinding" 
            contract="IService1" behaviorConfiguration="EndpBehavior"/>
       </service>
      </services>
    </system.serviceModel>
    

    As you can see in above config file, the EndPoint setting is changed, and EndPointBehaviors added to support WEBHTTP requests.

    Note: Endpoint settings done in the config works in conjunction with the WebInvoke attribute of the operation and the compatibility attribute set in ServiceType to support HTTP requests sent by jQuery.

    If it still can not solve your problem, please try to use the fillder to help you.

    #Fiddler:
    http://fiddler2.com/get-fiddler .

    Best Regards,
    Amy Peng


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 27, 2013 2:48 AM
    Moderator