【问题标题】:How to pass JSON string in UriTemplate using WCF without using Query string如何在不使用查询字符串的情况下使用 WCF 在 UriTemplate 中传递 JSON 字符串
【发布时间】:2017-12-24 07:14:11
【问题描述】:
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 时,我收到错误“错误请求”。

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

【问题讨论】:

  • 唯一的方法是使用webinvoke(post)而不是webget

标签: wcf c#-4.0 wcf-rest


【解决方案1】:

您必须将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);
        }
    });

【讨论】:

    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 2013-02-21
    • 2010-11-14
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    相关资源
    最近更新 更多