C# 使用ServiceStack反序列化数组
以下是我试图发布到ServiceStack web服务的内容:C# 使用ServiceStack反序列化数组,c#,javascript,jquery,json,
servicestack,C#,Javascript,Jquery,Json,
servicestack,以下是我试图发布到ServiceStack web服务的内容: $.ajax({ url: 'http://localhost:8092/profiles', type:'POST', data: { FirstName : "John", LastName : "Doe", Categories : [ "Category 1", "Category 2", "Category 3" ] }, succes
$.ajax({
url: 'http://localhost:8092/profiles',
type:'POST',
data: {
FirstName : "John",
LastName : "Doe",
Categories : [ "Category 1", "Category 2", "Category 3" ]
},
success: function (response) { $('#response').append('Success!'); },
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + ' Error: ' + thrownError);
},
dataType: "json"
});
这是目的地课程:
[Route("/profiles", "POST")]
public class ProfileRequest
{
public string FirstName{ get; set; }
public string LastName{ get; set; }
public string[] Categories{ get; set; }
}
正在填充除阵列之外的所有字段。ServiceStack不能处理如此复杂的对象,还是我遗漏了什么
注意:我已经看到了使用JSON.stringify()的建议,但该解决方案似乎仍然不起作用,事实上,它使情况变得更糟,因为没有数据被反序列化。ServiceStack可以处理这一点,但是您的jQuery
$.ajax
数据必须使用JSON.stringify()进行JSON字符串化
方法,并将contentType
设置为application/json
,否则jQuery将尝试将数据发布为application/x-www-form-urlencoded
,反序列化将失败
因此,您的请求应该是:
$.ajax({
网址:'http://localhost:8092/profiles',
类型:'POST',
数据:JSON.stringify({
名字:“约翰”,
姓:“Doe”,
类别:[“1类”、“2类”、“3类”]
}),
success:function(response){$('#response').append('success!');},
错误:函数(xhr、ajaxOptions、thrownError){
警报(xhr.status+'Error:'+thrownError);
},
contentType:“应用程序/json”,
数据类型:“json”
});
希望对您有所帮助。ServiceStack可以处理这个问题,但是您的jQuery
$.ajax
数据必须使用JSON.stringify()进行JSON字符串化
方法,并将contentType
设置为application/json
,否则jQuery将尝试将数据发布为application/x-www-form-urlencoded
,反序列化将失败
因此,您的请求应该是:
$.ajax({
网址:'http://localhost:8092/profiles',
类型:'POST',
数据:JSON.stringify({
名字:“约翰”,
姓:“Doe”,
类别:[“1类”、“2类”、“3类”]
}),
success:function(response){$('#response').append('success!');},
错误:函数(xhr、ajaxOptions、thrownError){
警报(xhr.status+'Error:'+thrownError);
},
contentType:“应用程序/json”,
数据类型:“json”
});
希望对您有所帮助。ServiceStack可以处理这个问题,但是您的jQuery
$.ajax
数据必须使用JSON.stringify()进行JSON字符串化
方法,并将contentType
设置为application/json
,否则jQuery将尝试将数据发布为application/x-www-form-urlencoded
,反序列化将失败
因此,您的请求应该是:
$.ajax({
网址:'http://localhost:8092/profiles',
类型:'POST',
数据:JSON.stringify({
名字:“约翰”,
姓:“Doe”,
类别:[“1类”、“2类”、“3类”]
}),
success:function(response){$('#response').append('success!');},
错误:函数(xhr、ajaxOptions、thrownError){
警报(xhr.status+'Error:'+thrownError);
},
contentType:“应用程序/json”,
数据类型:“json”
});
希望对您有所帮助。ServiceStack可以处理这个问题,但是您的jQuery
$.ajax
数据必须使用JSON.stringify()进行JSON字符串化
方法,并将contentType
设置为application/json
,否则jQuery将尝试将数据发布为application/x-www-form-urlencoded
,反序列化将失败
因此,您的请求应该是:
$.ajax({
网址:'http://localhost:8092/profiles',
类型:'POST',
数据:JSON.stringify({
名字:“约翰”,
姓:“Doe”,
类别:[“1类”、“2类”、“3类”]
}),
success:function(response){$('#response').append('success!');},
错误:函数(xhr、ajaxOptions、thrownError){
警报(xhr.status+'Error:'+thrownError);
},
contentType:“应用程序/json”,
数据类型:“json”
});
希望这能有所帮助。是的,这是我已经尝试过的方法之一,但结果是没有数据被反序列化,不仅仅是数组。看起来我使用的是3.9.56.0,但似乎仍然不起作用。看看我的C#对象,所有的值都是空的。如果我删除了JSON.stringify(),除了数组之外,所有的东西都会被填充。@SethMoore好的,我已经设置了一个测试项目,并让它开始工作。您需要设置
contentType:“application/json”
然后它就可以工作了。现在,这给了我一个错误“EndpointHandlerBase”|处理请求时出错:在Service ProfileController上找不到名为Options(ProfileRequest)或Any(ProfileRequest)的方法“是的,这是我已经尝试过的事情之一,但结果是没有数据被反序列化,不仅仅是数组。看起来我使用的是3.9.56.0,但似乎仍然不起作用。看看我的C#对象,所有的值都是空的。如果我删除了JSON.stringify(),除了数组之外,所有的东西都会被填充。@SethMoore好的,我已经设置了一个测试项目,并让它开始工作。您需要设置contentType:“application/json”
然后它就可以工作了。现在,这给了我一个错误“EndpointHandlerBase”|处理请求时出错:在Service ProfileController上找不到名为Options(ProfileRequest)或Any(ProfileRequest)的方法“是的,这是我已经尝试过的事情之一,但结果是没有数据被反序列化,不仅仅是数组。看起来我使用的是3.9.56.0,但似乎仍然不起作用。看看我的C#对象,所有的值都是空的。如果我删除了JSON.stringify(),除了数组之外,所有的东西都会被填充。@SethMoore好的,我已经设置了一个测试项目,并让它开始工作。您需要设置contentType:“application/json”
然后它就可以工作了。现在,这给了我一个错误“EndpointHandlerBase”|处理请求时出错:在服务ProfileControl上找不到名为Options(ProfileRequest)或Any(ProfileRequest)的方法