如何使用WCF在UriTemplate中传递JSON字符串而不使用查询字符串
我能够通过查询字符串传递json字符串。但是,当我想传递pass而不传递查询字符串时,我得到了错误如何使用WCF在UriTemplate中传递JSON字符串而不使用查询字符串,wcf,c#-4.0,wcf-rest,Wcf,C# 4.0,Wcf Rest,我能够通过查询字符串传递json字符串。但是,当我想传递pass而不传递查询字符串时,我得到了错误 http://localhost:51238/RestService.svc/FOSLoadingS1Opening?Data=[ { "Name":"Sachin", "City":"Bengalueu", "FimStatus":"false", "Deno1":"50", "Deno2":"100", "Deno3":"500", "Deno4":"2000", "IndtV
http://localhost:51238/RestService.svc/FOSLoadingS1Opening?Data=[
{
"Name":"Sachin",
"City":"Bengalueu",
"FimStatus":"false",
"Deno1":"50",
"Deno2":"100",
"Deno3":"500",
"Deno4":"2000",
"IndtVal1":"2500",
"IndtVal2":"5000" }]
当我传递上述URL时,我收到错误“错误请求”
我想在不使用查询字符串的情况下传递json字符串。请建议如何实现。您必须将
WebInvoke
与方法POST结合使用
http://localhost:51238/RestService.svc/FOSLoadingS1Opening/[
{
"Name":"Sachin",
"City":"Bengalueu",
"FimStatus":"false",
"Deno1":"50",
"Deno2":"100",
"Deno3":"500",
"Deno4":"2000",
"IndtVal1":"2500",
"IndtVal2":"5000" }]
现在,从客户端构造对象,并通过ajax调用发送json字符串化数据
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "SaveData/{id}")]
string SaveData(YourType typeObj);
唯一的方法是使用webinvoke(post)而不是webget
var obj = {
"Name":"Sachin",
"City":"Bengalueu",
"FimStatus":"false",
...
};
$.ajax({
type: "POST",
url: "http://localhost/wcf.svc/SaveData/0",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (data, status, jqXHR) {
alert("success..." + data);
},
error: function (xhr) {
alert(xhr.responseText);
}
});