发送参数到的jQuery阿贾克斯无效 [英] Sending Param to the Ajax of jQuery not effective

查看:141
本文介绍了发送参数到的jQuery阿贾克斯无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用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);
  });
});

这篇关于发送参数到的jQuery阿贾克斯无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆