可能重复:
How To Return Value From Code Behind To AJAX?如何从后面的代码将值返回给AJAX?
这是我的AJAX代码
$.ajax({
type: 'POST',
url: 'country_management.aspx/save',
cache: false,
data: "{'parameter':'paramValue'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);
if (data.d == "error") {
$('.success_box').hide();
$('.error_box').show();
}
else {
$('#name').val('');
$('.error_box').hide();
$('.success_box').show();
}
}
});
代码背后:
[WebMethod]
[ScriptMethod]
public static string save(string parameter)
{
string name = HttpContext.Current.Request.QueryString["name"].Trim();
return "error";
}
WR后在第一行中,return语句不会返回任何内容给AJAX。
你不是返回JSON,而是一个普通的字符串。看看这个http://stackoverflow.com/questions/5364343/asp-net-web-forms-json-return-result。 – Alex
你确定'QueryString [“name”]'没有返回'null'吗? – Rawling
你是说这行后出错吗? string name = HttpContext.Current.Request.QueryString [“name”]。Trim(); –