由于某些原因,我无法让ServiceStack序列化已发布的(serializeArray)表单数据。 json发布的是:
{"somestuff":"someData","formInput":[{"name":"1","value":"2"},...]}
我用来发布表单的jQuery:
var formData = $("form").serializeArray();
var data = {
someOtherFields: some data,
formInput: formData
};
$.ajax({
type: "post",
url: "/api/location",
data: data,
dataType: "json",
success: function(response) {
if (response.status == "success") {
scope.showForm = false;
scope.status = "successfully added message";
} else {
scope.status = response.message;
}
}
});
这是使用DTO发布到ServiceStack服务:
public class ServiceRequest
{
other atributes;
public List<ArraySerializeResult> FormInput { get; set; }
}
public class ArraySerializeResult
{
public string Name { get; set; }
public string Value { get; set; }
}
其他属性序列化正常但是formInput序列化为具有正确元素数量的列表,但所有Name和Value对都为null。
答案 0 :(得分:2)
SerivceStack希望您send your complex type格式为JSV而不是JSON。
您可以使用ServiceStack项目中的JSV.js格式化JSV中的请求而不是JSON,也可以实现自己的Request Binder来反序列化JSON。
public override void Configure(Container container)
{
RequestBinders.Add(typeof (ServiceRequest), (IHttpRequest httpReq) => {
var myRequest = new ServiceRequest();
// Deserialize the request here using ServiceStack.Text or whatever you like...
return myRequest;
});
}
答案 1 :(得分:1)
你应该添加&#34; contentType&#34;您的ajax请求的选项。使用content-type&#34; application / json&#34;,servicestack可以预期json数组
contentType: "application/json; charset=utf-8"
所以它看起来像这样
$.ajax({
type: "post",
url: "/api/location",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
if (response.status == "success") {
scope.showForm = false;
scope.status = "successfully added message";
} else {
scope.status = response.message;
}
}
});