Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: $.ajax(...).success is not a function

I'm new to jQuery and using a little old tutorial on node.js that uses this snippet :

$(function () {         var roomId;      $.ajax({         type: "GET",         url: "/api/rooms"     }).success(function (rooms) {          roomId = rooms[0].id;         getMessages();         $.each(rooms, function (key, room) {             var a = '<a href="#" data-room-id="' + room.id + '" class="room list-group-item">' + room.name + '</a>';             $("#rooms").append(a);         });      });      [...]         }); 

However I get this error

Uncaught TypeError: $.ajax(...).success is not a function

at }).success(function (rooms) {

I'm wondering what can be wrong here?

like image 222
Karlom Avatar asked Aug 19 '16 14:08

Karlom


People also ask

How do I fix uncaught Typeerror Ajax is not a function?

To solve the "$. ajax is not a function" error, add the regular jQuery script to the page and remove the slim version. The slim jQuery version does not include the ajax function.

What is success function Ajax?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.


2 Answers

The call to ajax should look like:

$.ajax({     type: "GET",     url: "/api/rooms",     success: function (rooms) {       } }); 

You don't method chain the success function, it is one of the entries in the dictionary argument.

like image 76
JCollerton Avatar answered Sep 22 '22 22:09

JCollerton


Your code is correct there is no problem with it

but you might be including the new jquery library which doesn't allow .success() method

for newer version of jquery use

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  <script>     $.ajax({         type: "GET",         url: "/api/rooms",         success: function (rooms) {          }     }); </script> 

and if you are using old jquery the .success() method would run without any problem

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  <script>     $.ajax({          url: "/api/rooms",         method: "GET",         data: {'datavar': datavalue}      }).success(function (rooms) {          console.log("successfully run ajax request..." + rooms);      }).done(function () {          console.log("I am from done function");      }).fail(function () {          console.log("I am from fail function.");      }).always(function () {          console.log("I am from always function");      }); </script> 
like image 36
Shailesh Sonare Avatar answered Sep 25 '22 22:09

Shailesh Sonare