2011-05-05 163 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的替代品。