Ajax传递Json数据之后无法用string类型进行接收,百分送上
用jqueryAjaxPost了一个json数据.
大概的demo简化了一下变成这样
var json='{ "menu":[{"Role_ID":"2","Customer_ID":"155","Brands":"Chloe;","Country_ID":"96;"},{"Role_ID":"-1","Customer_ID":"497","Brands":"Chloe;","Country_ID":"96;"}]}';
$.ajax({
url: "RoleFunc.aspx/Convert",
type: "POST",
contentType: "application/json",
dataType: 'json',
data: '{"strJson":' + json+ '}',
success: function(result) {
alert(result);
},
error: function() {
alert("error");
}
});
我想用后台去处理这一条js数据.但是我用string作为参数的时候会出现
No parameterless constructor defined for type of 'System.String'.
这样的错误.我自定义了一个Class之后
public class CustomerRole
{
public string Role_ID { get; set; }
public string Customer_ID { get; set; }
public string Brands { get; set; }
public string Country_ID { get; set; }
}
后台测试方法
[WebMethod]
public static string Convert(string strJson)//error
{
return strJson;
}
[WebMethod]
public static string Convert(CustomerRole strJson)//可以,但是这个strJson的obj为空
{
return strJson;
}
将这个Class作为参数.则可以接收,但是自然的.obj为空
请教一下各位.如何把这条json作为string来post.或者让自定义的class可以接受处理这条传递过来的json?
谢谢各位 jquery ajax asp.net json
[解决办法]
inference as following
01.$.ajax({
02. type: "POST",
03. url: "WebService.asmx/WebMethodName",
04. data: <span style="color: rgb(255, 102, 102);">"</span>{'fname':'dave', 'lname':'ward'}<span style="color: rgb(255, 0, 0);">"</span>,
05. contentType: "application/json; charset=utf-8",
06. dataType: "json"
07.});
[解决办法]
var strJson = { Users: [{ Id: 1, Name: "Ahoo1" }, { Id: 2, Name: "Ahoo2"}] }
var url="@Url.Action("DesJson")";
$.ajax({
url: url,
dataType: "json",
data: { json: JSON.stringify(strJson.Users) },//将数组对象持久化为string
success: function (data) {
alert(data);
},
error: function (data) {
alert(data);
}
});

测试了一下,应该是这样的