ajax jquery post方法

我有如下所示的url DataProcessor.aspx的ajax请求,如何编写asp.net c#代码以从请求中提取json数据并在DataProcessor.aspx页面中显示

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("Record saved successfully."); } else { record.html(returnText); $("#divMsg").html("Record not saved successfully."); } }); }); 

而不是发布json并手动解析它,另一种选择是创建脚本服务并使用该服务。 这样做的好处是重量更轻(你不必经历整个aspx页面生命周期),并让.Net为将json解析成一个对象做了大量工作。

基本上你只需要创建一个简单的Web方法,它接受两个参数,ItemName,CategoryID,RecordID。 应用脚本服务装饰器或方法让.Net知道您想通过JSON POST与它进行交互,并将post的地址更改为“YourWebService.asmx / YourWebMethodName”

要处理对显示的更新,请创建一个对象,以便从包含要在页面上更新的数据的函数返回并返回该对象。 在AJAX调用的成功案例中处理返回值(上面函数中的’response’参数)并相应地更新显示(不知道更多关于如何或想要更新的内容,我无法真正帮助进一步)。