2011-05-05 168 views
3

我创建了一个客户 JSON对象,它下面有值:结果显示为空

{"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]","EmailType":"Personal"},{"Email":"[email protected]","EmailType":"Work"}],"Phone":[{"Phone":"1231231234","PhoneType":"Mobile"},{"Phone":"1231232345","PhoneType":"Work"}]} 

我需要在处理程序相关的数据/ CustomerHandler.ashx做一些数据库操作。 我的AJAX调用如下:

$.ajax({ 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     url: "Handlers/CustomerHandler.ashx", 
     data: Customer, 
     dataType: "json", 
     success: insertCustomerCallback 
    }); 

Dim customerJSON As String = HttpContext.Current.Request.Form("Customer")显示为空。

回答

1

我想我找到了。 我不得不作出AJAX调用如下:

Customer = JSON.stringify(Customer); 
$.ajax({ 
    type: "POST", 
    contentType: "application/x-www-form-urlencoded", 
    url: "Handlers/CustomerHandler.ashx?Operation=Insert", 
    data: Customer, 
    dataType: "json", 
    success: insertCustomerCallback 
}); 

,并使用以下代码来获取数据

昏暗customerJSON作为字符串= HttpContext.Current.Request.Form(0)的ToString()

感谢, Sharmin

2

您可以发送客户从json2.js使用JSON.stringify方法的JSON对象:

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]", "EmailType": "Personal" }, { "Email": "[email protected]", "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 

正如你可以使用一个script enabled WebMethod的替代品。