发送参数到的jQuery阿贾克斯无效参数、jQuery、阿贾克斯

2023-09-10 17:41:54 作者:果味小可爱

我想要使用jQuery的阿贾克斯,我下面这个code,虽然我在Firebug它不工作没有错误,它似乎在code的功能的背后没有得到任何PARAMS。

I'm trying to use Ajax of jQuery, I have this code below and although I get no error in firebug it's not working, It seems the function in code behind doesn't get any params.

(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));                
}

以下工作code,但因为我想用它在IE 6我必须使用上面的code。

the code below work but since I wanna use it in IE 6 I have to use the code above.

 $(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"

     });
 });

});

在这里你觉得我做错了吗?

where do you think i'm doing wrong?

推荐答案

如果您的问题仅是IE6没有JSON.stringify方法,除了可以使用的 json2.js从道格拉斯Crockford的,然后你的第二个样品应能按预期在IE6了。

If your issue only is that IE6 has no JSON.stringify method, than you can use json2.js from Douglas Crockford and then your second sample should work as expected in IE6 too.

$(function () {
  $("#S2").click(function 
     $("#t2").toggle("fast");
     $("#P2").toggle("fast");
     $("#S2").css("background-color","White");
     var ID = $("#HiddenField2").attr("Value");
     var myJSONText = JSON.stringify({ Key: ID });
     $.ajax({
         type: "POST",
         url: "viewMessages.aspx/readen",
         data: myJSONText,
         contentType: "application/json",
         dataType: "json"
     });
  });
});

另一种方法是不使用JSON数据类型,然后PARAMS应序列作为常规查询字符串。

Another approach is do not use 'json' datatype, and then params should be serialized as regular query string.

$(function () {
  $("#S2").click(function 
     $("#t2").toggle("fast");
     $("#P2").toggle("fast");
     $("#S2").css("background-color","White");
     var ID = $("#HiddenField2").attr("Value");
     var params = { Key: ID };
     $.post("viewMessages.aspx/readen", params);
  });
});
 
精彩推荐