一段时间以来,我所有的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 对象,但Msg
和Keys
属性为空。
通过以下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);
}));
});
我有什么误解?