dimanche 26 juin 2016

JSON Data not able to pass to the REST Service


I am learning web development and stuck at one issue for two days. I am not able to Pass JSON data from my Form to the REST Service.

Below is what I have tried till now.

My Sample HTML Form:

  <!DOCTYPE html>
    <html>
    <body>
    <form>
    <input id="btnGetResponse" type="button" value="ClickMe!"/> 
    </form>
    <div> </div>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
    $("#btnGetResponse").click(function()
    {
    $.ajax({
        type: "POST",
        url: "http://localhost:51349/SMS_Rest.svc/v1/usercheckboxes",
         data: {
           "checkBoxProp" : 1
        },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response)
        {
            alert('hello');
        },
        failure: function(response) 
        {
            alert('fail');
        }
       });
    });
</script>
</body>
</html>

My REST Service in VB.net

<OperationContract(Name:="CheckBoxDetails")>
    <WebInvoke(Method:="POST", BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="usercheckboxes")>
    Function CheckBoxDetails(requestData As CheckBoxDataObjRequest) As CheckBoxDataObjResponse

Sample Implementation:

Public Function CheckBoxDetails(requestData As CheckBoxDataObjRequest) As CheckBoxDataObjResponse Implements iSMS_Rest.CheckBoxDetails
        Dim res As New CheckBoxDataObjResponse
        Return res
    End Function

Sample Object File:

  Imports Microsoft.VisualBasic

    <DataContract>
    Public Class CheckBoxDataObjRequest

        <DataMember(Name:="checkBoxProp")>
        Private checkBox As Integer
        Public Property checkBoxProp() As Integer
            Get
                Return checkBox
            End Get
            Set(ByVal value As Integer)
                checkBox = value
            End Set
        End Property


    End Class

    <DataContract>
    Public Class CheckBoxDataObjResponse

        <DataMember(Name:="checkBoxResponseProp")>
        Private checkBoxResponse As Integer
        Public Property checkBoxResponseProp() As Integer
            Get
                Return checkBoxResponse
            End Get
            Set(ByVal value As Integer)
                checkBoxResponse = value
            End Set
        End Property

    End Class

Kindly help me. I have put break point too in my Rest Service But Neither the breakpoint nor the data is received. Error I get is :

 http://localhost:51349/SMS_Rest.svc/v1/usercheckboxes 500 (System.ServiceModel.ServiceActivationException)

My Web Config File:

 <service name="SMS_Rest">
        <endpoint address="v1" binding="webHttpBinding" contract="iSMS_Rest" bindingConfiguration="bw" behaviorConfiguration="web" name="restful"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/"/>
          </baseAddresses>
        </host>
      </service>

Aucun commentaire:

Enregistrer un commentaire