在REST中使用POST传递JSON时出现400错误请求

时间:2016-06-26 05:07:16

标签: javascript json ajax vb.net rest

我有一个简单的HTML文件。我想从前端向Rest Service发送一些数据,但我无法这样做。我错过了什么。请帮助我。如何传递JSON以及我需要传递的格式。

HTML文件:

 <!DOCTYPE html>
    <html>
    <body>
    <form>
    <input id="btnGetResponse" type="button" value="ClickMe!"/> 
    </form>
    <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",
// IS this URL Correct
        url: "http://localhost:51349/SMS_Rest.svc/v1/usercheckboxes",
// IS this Data Json Correct Format
        data: {
          checkBoxProp : "hello"
        },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response)
        {
            alert('hello');
        },
        failure: function(response) 
        {
            alert('fail');
        }
       });
    });
    </script>
    </body>
    </html>

示例REST代码:

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

REST类:

Imports Microsoft.VisualBasic

<DataContract>
Public Class CheckBoxDataObjRequest

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


End Class

<DataContract>
Public Class CheckBoxDataObjResponse

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

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

Web配置:

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

0 个答案:

没有答案