我正在学习使用jquery ajax来处理JSON。我写了一个演示代码。HTMLCODE
$(function () {
$("#add").click(function () {
var json = '{ "str":[{"Role_ID":"2","Customer_ID":"155","Brands":"Chloe;","Country_ID":"96;"}]}';
$.ajax({
url: "func.aspx/GetJson",
type: "POST",
contentType: "application/json",
dataType: 'json',
data: json,
success: function (result) {
alert(result);
},
error: function () {
alert("error");
}
});
});
});
<div>
<input type="button" value="add" id="add" />
</div>我得到了一个输入,并将脚本函数绑定到它,现在问题来了。我的C#函数就是这样的。
[WebMethod]
public static string GetJson(object str)
{
return str.ToString();//good for work
}
[Serializable]
public class TestClass
{
public TestClass()
{
}
public TestClass(string role_id, string customer_id, string brands, string countryid)
{
this.Role_ID = role_id;
this.Customer_ID = customer_id;
this.Brands = brands;
this.Country_ID = countryid;
}
public string Role_ID { get; set; }
public string Customer_ID { get; set; }
public string Brands { get; set; }
public string Country_ID { get; set; }
}当我使用公共静态字符串GetJson(object str)时,一切都很好。当我尝试使用我自己的类TestClass时。firebug告诉我“数组的反序列化不支持类型'TestClass‘。”firebug body可以提供帮助:XD
发布于 2013-11-05 23:50:23
这就是我使用WCF WCF服务来做这件事时的样子。希望它能帮助你。如果你需要进一步的澄清,请告诉我。
脚本:
var data = {
emailAddress: emailAddress,
firstName: firstName,
lastName: lastName,
groups: groups
};
$.ajax({
type: "POST",
url: Client.svc/Subscribe",
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json; charset=utf-8",
processdata: true,
success: function (result) {
//do something
}
});服务:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[BasicHttpBindingServiceMetadataExchangeEndpoint]
public class Client : IClient
{
public bool Subscribe(string emailAddress, string firstName, string lastName, string[] groups)
{
//do something
return true;
}
}https://stackoverflow.com/questions/19791566
复制相似问题