Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 () {                  }             });         });     }); 
like image 556
ePezhman Avatar asked Oct 13 '11 19:10

ePezhman


People also ask

What is toJSON function?

The toJSON() method returns a date object as a string, formatted as a JSON date.

Why res JSON is not a function?

Conclusion # The "response. json is not a function" error occurs when we call the json() method on an object that is not the Response object that resolves from the promise the fetch() method returns, or call the json() method on the return value from calling an axios method.

When to use toJSON?

The toJSON() function is useful for making sure ES6 classes get serialized correctly. For example, suppose you have a custom JavaScript error class. By default, JavaScript isn't great with serializing errors. The below script prints {"status":404} , no error message or stack trace.


2 Answers

That's correct.

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

like image 143
Matt Ball Avatar answered Sep 22 '22 14:09

Matt Ball


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

like image 38
Lekensteyn Avatar answered Sep 20 '22 14:09

Lekensteyn