jQuery 收到此错误“$.toJSON 不是函数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7759619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:14:47  来源:igfitidea点击:

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

jqueryajaxjson

提问by ePezhman

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

我正在尝试使用 jQuery 和 Ajax,我使用这种方法。但我收到错误 $.toJSON is not a function in my firebug 。问题出在哪儿?我使用 jquery 1.3.2 。谢谢

  $(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 () {

                }
            });
        });
    });

回答by Matt Ball

That's correct.

没错。

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

没有$.toJSON()函数:http: //api.jquery.com/jQuery.toJSON。也许你想JSON.stringify()改用。

回答by Lekensteyn

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

您需要在您的页面上包含jquery-json来自http://code.google.com/p/jquery-json/的库。

回答by pedferdev

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

我正在学习 jquery 并且我有同样的问题。我的错误 - 在这行代码中打断了我:

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:

但请帮助我知道你想要你的方法readen

url: "viewMessages.aspx/readen"

网址:“viewMessages.aspx/readen”

回答by Yassine Khachlek

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

您已经包含 jquery,没有理由使用任何其他库,只需使用 $.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/

来源:http: //api.jquery.com/jquery.parsejson/

回答by Vikas Naranje

Just use

只需使用

data: params,

OR

或者

var params = {key:ID},