Javascript 400在REST中使用POST传递JSON时请求错误
我有一个简单的HTML文件。我想将一些数据从前端发送到Rest服务,但我无法这样做。我错过了什么吗。请帮帮我。如何传递JSON以及需要传递的格式 HTML文件:Javascript 400在REST中使用POST传递JSON时请求错误,javascript,json,ajax,vb.net,rest,Javascript,Json,Ajax,Vb.net,Rest,我有一个简单的HTML文件。我想将一些数据从前端发送到Rest服务,但我无法这样做。我错过了什么吗。请帮帮我。如何传递JSON以及需要传递的格式 HTML文件: <!DOCTYPE html> <html> <body> <form> <input id="btnGetResponse" type="button" value="ClickMe!"/> </form> &l
<!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>
休息班:
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
导入Microsoft.VisualBasic
公共类CheckBoxDataObjRequest
私有复选框作为字符串
公共属性checkBoxProp()作为字符串
得到
返回复选框
结束
设置(ByVal值作为字符串)
复选框=值
端集
端属性
末级
公共类CheckBoxDataObjResponse
私有复选框作为字符串
公共属性checkBoxProp()作为字符串
得到
返回复选框
结束
设置(ByVal值作为字符串)
复选框=值
端集
端属性
末级
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
函数CheckBoxDetails(请求数据作为CheckBoxDataObjRequest)作为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>
我认为只有服务名称就足够了,当我发送空JSON时,您不需要主机和名称。我的意思是,当我在Ajax中保存数据:{}
时,在这种情况下,我可以在Rest服务中找到断点。请检查数据格式和URL。其余的事情我都做对了吗?我想只有服务名就足够了。当我发送一个空的JSON时,你不需要主机名。我的意思是,当我在Ajax中保存数据:{}
时,在这种情况下,我可以在Rest服务中找到断点。请检查数据格式和URL。其余的事情我都做对了吗?
<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>