开发者

Result is shown as empty

开发者 https://www.devze.com 2023-03-03 12:16 出处:网络
I have a Customer JSON object created which has value as below: {\"Title\":\"Mr\",\"FirstName\":\"S\",\"LastName\":\"J\",\"Birthday\":\"01/01/2011\",\"Address\":[{\"Line1\":\"Line1\",\"Line2\":\"Line

I have a Customer JSON object created which has value as below:

{"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":"s.j@sj.com","EmailType":"Perso开发者_运维百科nal"},{"Email":"s.j1@company.com","EmailType":"Work"}],"Phone":[{"Phone":"1231231234","PhoneType":"Mobile"},{"Phone":"1231232345","PhoneType":"Work"}]}

I need to get this data in the Handlers/CustomerHandler.ashx to do some DB operations. My AJAX call is as follows:

  $.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")is shown as empty.


You might send the customer as a JSON object using the JSON.stringify method from json2.js:

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": "s.j@sj.com", "EmailType": "Personal" }, { "Email": "s.j1@company.com", "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) {

    }
});

and on the generic handler read it from the request stream:

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

As an alternative you could use a script enabled WebMethod.


I guess I found it. I had to make the AJAX call as follows:

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
});   

and use the below code to get the data

Dim customerJSON As String = HttpContext.Current.Request.Form(0).ToString()

Thanks, Sharmin


You might consider using an ASMX "ScriptService" instead of the raw HttpHandler. Using a ScriptService has the handy benefit of automatically deserializing JSON input to .NET objects, which looks like it would be helpful in your scenario.

0

精彩评论

暂无评论...
验证码 换一张
取 消