JSON数据无法传递给REST服务

时间:2016-06-25 16:18:53

标签: javascript jquery ajax vb.net rest

我正在学习网络开发,并在一个问题上坚持了两天。我无法将JSON数据从我的表单传递到REST服务。

以下是我迄今为止所尝试的内容。

我的示例HTML表单:

  <!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>

我在VB.net中的REST服务

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

示例实施:

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

示例对象文件:

  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

请帮助我。我在休息服务中也提出了断点但是没有收到断点和数据。我得到的错误是:

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

我的网站配置文件:

 <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>

0 个答案:

没有答案