views:

27

answers:

1

i have ajax request to url DataProcessor.aspx like shown below ,how can i write asp.net c# code to extract the json data from request and display in DataProcessor.aspx page

var json = "{'ItemName':'" + escape(item.val()) + "','CategoryID':'" + category.val() + "','RecordID':'" + record.val() + "'}";
            alert(escape(item.val()));
            alert(category.val());
            alert(record.val());
            var ajaxPage = "DataProcessor.aspx?Save=1"; //this page is where data is to be retrieved and processed
            var options = {
                type: "POST",
                url: ajaxPage, 
                data: json,
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: false,
                success: function(response) {
                    //alert("success: " + response);
                },
                error: function(msg) { alert("failed: " + msg); }
            };

            //execute the ajax call and get a response
            var returnText = $.ajax(options).responseText;
            if (returnText == 1) {

                record.html(returnText);
                $("#divMsg").html("<font color=blue>Record saved successfully.</font>");
            }
            else {
                record.html(returnText);
                $("#divMsg").html("<font color=red>Record not saved successfully.</font>");


            }
        });
    });
+1  A: 

Rather than post the json and parse it manually, another option would be to create a script service and consume that service. This has the advantage of being lighter weight (you won' t have to to go through the whole aspx page lifecycle) and let's .Net do the heavy lifting for parsing the json into an object.

Basically you will just need to create a simple web method that takes two arguments, ItemName, CategoryID, RecordID. Apply the Script Service decorator or to the method to let .Net know you want to interact with it via JSON POSTs, and change the address of your post to "YourWebService.asmx/YourWebMethodName"

To handle updates to the display, create an object to return from your function that contains the data you want to update on the page and return it. Process the return value (the 'response' argument in your function above) in the success case of your AJAX call and update the display accordingly (not knowing more about how or what you want to update, I can't really help further on that).

Tarwn