开发者

getting this error " $.toJSON is not a function "

开发者 https://www.devze.com 2023-04-12 17:00 出处:网络
I\'m trying to use jQuery and Ajax and I use this method. but I get the error $.toJSON is not a function in my firebug . where is the problem? I use jquery 1.3.2 . thanks

I'm trying to use jQuery and Ajax and I use this method. but I get the error $.toJSON is not a function in my firebug . where is the problem? I use jquery 1.3.2 . thanks

  $开发者_如何学运维(document).ready(function () {
        $("#S1").click(function 
            () {
            $("#t1").slideToggle("fast");
            $("#S1").css("background-color", "yellow");
            var ID = $("#HiddenField2").attr("Value");
            var params = new Object();
            params.Key = ID;
            $.ajax({
                type: "POST",
                url: "viewMessages.aspx/readen",
                data: $.toJSON(params),
                contentType: "application/json",
                dataType: "json",
                success: function () {

                }
            });
        });
    });


That's correct.

There is no $.toJSON() function: http://api.jquery.com/jQuery.toJSON. Perhaps you want to use JSON.stringify() instead.


You need to include the jquery-json library from http://code.google.com/p/jquery-json/ on your page.


I'm learning jquery and I have the same problem. My mistake - in this line of code which interrupted me:

var params = $.toJSON(request);

After replacing it with this code:

var params = JSON.stringify(request);

I did not get any error.

But please help me know how you want your method readen :

url: "viewMessages.aspx/readen"


You are already included jquery, there is no reason to use any other library, just use $.parseJSON(a_json_string) :

var error_json_string = '{"error":{"message":"message blablabla","type":"","code":""}}';
var error_json_obj = $.parseJSON(error_json_string);

alert(error_json_obj.error.message);

Source : http://api.jquery.com/jquery.parsejson/


Just use

data: params,

OR

var params = {key:ID},
0

精彩评论

暂无评论...
验证码 换一张
取 消