The result is shown as empty.

I have a JSON Client object that matters, as shown 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":"Personal"},{"Email":"s.j1@company.com","EmailType":"Work"}],"Phone":[{"Phone":"1231231234","PhoneType":"Mobile"},{"Phone":"1231232345","PhoneType":"Work"}]}

I need to get this data in Handlers / CustomerHandler.ashx in order to perform some database 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") displayed as empty.

+3
source share
3 answers

You can send the client 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 in the general 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

Alternatively, you can use a script with WebMethod enabled .

+2
source

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

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

, Sharmin

+1

You can use ASMX "ScriptService" instead of the raw HttpHandler. Using ScriptService has the useful advantage of automatically deserializing JSON input for .NET objects , which looks like it would be useful in your script.

0
source

All Articles