您可以使用 JSON.stringify 方法将客户作为 JSON 对象发送json2.js http://www.json.org/js.html:
var Customer = { "Title": "Mr", "FirstName": "S", "LastName": "J", "Birthday": "01/01/2011", "Address": [{ "Line1": "Line1", "Line2": "Line2", "City": "City", "State": "State", "Zip": "00000", "County": "0000" }, { "Line1": "Line11", "Line2": "Line21", "City": "City1", "State": "State1", "Zip": "11111", "County": "1111"}], "Email": [{ "Email": "[email protected] /cdn-cgi/l/email-protection", "EmailType": "Personal" }, { "Email": "[email protected] /cdn-cgi/l/email-protection", "EmailType": "Work"}], "Phone": [{ "Phone": "1231231234", "PhoneType": "Mobile" }, { "Phone": "1231232345", "PhoneType": "Work"}] };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Handlers/CustomerHandler.ashx",
data: JSON.stringify(Customer),
dataType: "json",
success: function (result) {
}
});
并在通用处理程序上从请求流中读取它:
Dim customer = New Byte(context.Request.InputStream.Length - 1) {}
context.Request.InputStream.Read(customer, 0, customer.Length)
Dim customerJSON = Encoding.UTF8.GetString(customer)
// TODO: deserialize the JSON back to a Customer object
作为替代方案,您可以使用启用脚本的 WebMethod http://dotnetslackers.com/articles/ajax/Using-jQuery-with-ASP-NET.aspx.