如何在不使用查询字符串的情况下使用 WCF 在 UriTemplate 中传递 JSON 字符串

How to pass JSON string in UriTemplate using WCF without using Query string

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"  }]

我能够将 json 字符串与查询字符串一起传递。但是,当我想传递没有查询字符串的传递时,我收到了错误。

http://localhost:51238/RestService.svc/FOSLoadingS1Opening/[
{ 
"Name":"Sachin", 
"City":"Bengalueu", 
"FimStatus":"false", 
"Deno1":"50", 
"Deno2":"100", 
"Deno3":"500", 
"Deno4":"2000", 
"IndtVal1":"2500", 
"IndtVal2":"5000"  }]

当我超过 URL 时出现错误 "Bad Request"。

我想在不使用查询字符串的情况下传递 json 字符串。请建议如何实现。

您必须将 WebInvoke 与方法 POST 一起使用。

[OperationContract]
[WebInvoke(Method = "POST",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "SaveData/{id}")]
string SaveData(YourType typeObj);

现在从客户端构建对象并通过 ajax 调用发送 json 字符串化数据。

    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);
        }
    });