您可以通过多种方法来执行此操作。这将作为一个示例。
您可以为您的jQuery代码编写如下代码:
urlToHandler = 'handler.ashx';jsonData = '{ "dateStamp":"2010/01/01", "stringParam": "hello" }';$.ajax({ url: urlToHandler, data: jsonData, dataType: 'json', type: 'POST', contentType: 'application/json', success: function(data) {setAutocompleteData(data.responseDateTime); }, error: function(data, status, jqXHR) {alert('There was an error.'); } }); // end $.ajax接下来,您需要在ASP.net项目中创建一个“通用处理程序”。在您的通用处理程序中,用于
Request.Form读取作为json传递的值。通用处理程序的代码如下所示:
[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class handler : IHttpHandler , System.Web.SessionState.IReadOnlySessionState{ public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; DateTime dateStamp = DateTime.Parse((string)Request.Form["dateStamp"]); string stringParam = (string)Request.Form["stringParam"]; // Your logic here string json = "{ "responseDateTime": "hello hello there!" }"; context.Response.Write(json); }看看如何解决。它会让您入门!
更新: 我将此代码发布在CodeReview
StackExchange上:https
://prereview.stackexchange.com/questions/3208/basic-simple-asp-net-jquery-
json-example



