【发布时间】:2010-08-24 09:37:27
【问题描述】:
我有 ajax 请求到 url DataProcessor.aspx 如下所示,我如何编写 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("<font color=blue>Record saved successfully.</font>");
}
else {
record.html(returnText);
$("#divMsg").html("<font color=red>Record not saved successfully.</font>");
}
});
});
【问题讨论】:
-
有关将 JSON 数据解析为 .NET 对象的讨论,请参阅 stackoverflow.com/questions/423294/…。