【问题标题】:Sending a complex object to a service stack using a GET request使用 GET 请求将复杂对象发送到服务堆栈
【发布时间】:2012-10-29 20:05:38
【问题描述】:

一段时间以来,我所有的ServiceStack 服务一直使用POST 动词来处理客户端发送的传入请求。不过,在这个特定场景中,我想使用 GET 动词,并且我希望能够传递一个相当复杂的对象(例如,包含数组)。

这是我的 ServiceStack 代码:

[Route("Test")]
public class TestRequest : IReturn<TestResponse>
{
    public Criteria Criteria { get; set; }
}

public class Criteria
{
    public string Msg { get; set; }
    public string[] Keys { get; set; }
}

[DataContract]
public class TestResponse
{
    [DataMember]
    public string Text { get; set; }

    [DataMember]
    public ResponseStatus ResponseStatus { get; set; }
}

public class TestService : ServiceInterface.Service, IGet<TestRequest>, IPost<TestRequest>
{
    public object Get(TestRequest request)
    {
        return HandleRequest(request);
    }

    public object Post(TestRequest request)
    {
        return HandleRequest(request);
    }

    private object HandleRequest(TestRequest request)
    {
        if (request == null) throw new ArgumentNullException("request");

        if (request.Criteria == null)
            throw new ArgumentException("Criteria is null.");

        return new TestResponse
        {
            Text =
                String.Format(
                    "Text: {0}. Keys: {1}",
                    request.Criteria.Msg,
                    String.Join(", ", request.Criteria.Keys ?? new string[0]))
        };
    }
}

由具有以下 jQuery 代码的 HTML 应用程序使用:

$(document).ready(function () {
    $.when($.ajax({
        url: '/Test',
        type: 'get',
        dataType: 'json',
        contentType: 'application/json',
        data: {
            "criteria": JSON.stringify({
                "msg": "some message",
                "keys": ["key1", "key2"]
            })
        }
    }).done(function (response) {
        console.log(response);
    }).fail(function (response) {
        console.log(response);
    }));
});

我的 Criteria 对象已创建,但 MsgKeys 属性为空。

使用以下POST 示例,应用程序按预期工作:

$(document).ready(function () {

        $.when($.ajax({
            url: '/Test',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify({
                "criteria": {
                    "msg": "some message",
                    "keys": ["key1", "key2"]
                }
            })

        }).done(function (response) {
            console.log(response);
        }).fail(function (response) {
            console.log(response);
        }));
    });

我误会了什么?

【问题讨论】:

  • 实际发布的 JSON 是什么?例如使用 Fiddler 或 WireShark 找出答案。
  • 被发布的 JSON 是:请求 URL:localhost:1770/… 导致:条件:{"msg":"some message","keys":["key1","key2"]}

标签: c# jquery .net web-services servicestack


【解决方案1】:

注意:您不能将 JSON 字符串与 JSON 对象混合和匹配(即,在 C# 中键入 POCO)。

您正在尝试发送在 JSON 字符串中转义的序列化 JSON 字符串,例如:

"{\"msg\":..."

连接到期望 JSON 对象的 POCO,例如:

{"msg":...

如果条件是字符串,例如:

public class TestRequest : IReturn<TestResponse>
{
    public string Criteria { get; set; }
}

它应该可以工作,否则您需要更改您的 JSON 请求以发送 JSON 对象,而不是序列化 + 转义为 JSON 字符串的 JSON 对象。

【讨论】:

    【解决方案2】:

    当您将 JSON.stringify 与 GET 请求一起使用时,查询字符串的格式不正确...

    【讨论】:

      猜你喜欢
      • 2013-10-20
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多