c# - post data to aspx page and return json object -
i trying data values page without webmethod
, , trying response json object. how can that?
<script type="text/javascript"> function foo() { $.ajax({ type: "post", url: 'foo.aspx?calendaruc=update', data: "{ 'x': '5', 'y': '6'}", contenttype: "application/json; charset=utf-8", datatype: "json", beforesend: function () { }, success: function (data) { console.log(data); } }); } </script> <a href="#" onclick="foo()">click</a> protected void page_load(object sender, eventargs e) { if (request.params["calendaruc"] != null) { if (request.params["calendaruc"] == "update") { var x = request.params["x"]; var y = request.params["y"]; //do stuff response.clear(); response.contenttype = "application/json"; response.write(new {result = true, message = "hello" }); response.end(); } } }
you coded right. ajax part correct.
c# part done also, forgot serialize returning object this:
var ser = new javascriptserializer().serialize(new { result = true, message = "hello" });
so page_load
method should this:
protected void page_load(object sender, eventargs e) { if (request["calendaruc"] != null && request["calendaruc"] == "update") { var x = request["x"]; var y = request["y"]; //do stuff var ser = new javascriptserializer().serialize(new { result = true, message = "hello" }); response.clear(); response.contenttype = "application/json"; response.write(ser); response.end(); } } }
i hope helps.
Comments
Post a Comment