我正在尝试使用 jQuery 的 Ajax,我在下面有这段代码,虽然我在 firebug 中没有收到任何错误,但它不起作用,似乎代码后面的函数没有获得任何参数。
(document).ready(function () {
$("#S1").click(function
() {
$("#t1").toggle("fast");
$("#P1").toggle("fast");
$("#S1").css("background-color", "White");
var ID = $("#HiddenField1").attr("Value");
var params = { 'Key': ID };
$.ajax({
type: "POST",
url: "viewMessages.aspx/readen",
data: params,
dataType: "json"
});
});
});
下面是代码
[WebMethod(EnableSession = false)]
public static void readen(string Key)
{
DBController db = new DBController();
db.ReadenMes(Convert.ToInt32(Key));
}
下面的代码可以工作,但因为我想在 IE 6 中使用它,所以我必须使用上面的代码。
$(document).ready(function () {
$("#S2").click(function
() {
$("#t2").toggle("fast");
$("#P2").toggle("fast");
$("#S2").css("background-color","White");
var ID = $("#HiddenField2").attr("Value");
var params = new Object();
params.Key = ID;
var myJSONText = JSON.stringify(params);
$.ajax({
type: "POST",
url: "viewMessages.aspx/readen",
data: myJSONText,
contentType: "application/json",
dataType: "json"
});
});
});
你认为我哪里做错了?
最佳答案
3 mistakes to avoid when using jQuery with ASP.NET AJAX
$(document).ready(function () {
$("#S1").click(function
() {
$("#t1").toggle("fast");
$("#P1").toggle("fast");
$("#S1").css("background-color", "White");
var ID = $("#HiddenField1").val();
var params = "{ 'Key':'" + ID + "'}"; //changes here
$.ajax({
type: "POST",
url: "viewMessages.aspx/readen",
data: params,
dataType: "json",
contentType: "application/json; charset=utf-8"
});
});
});
关于c# - 将参数发送到 jQuery 的 Ajax 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7894223/