Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot read property 'ajax' of undefined

I tried deleting an item from a table with AJAX via a POST call.

///// DELETE INDIVIDUAL ROW IN A TABLE /////
jQuery('.stdtable .delete').live('click', function (e) {
//var newsId1 = $(this).attr("title");

e.preventDefault();

var p = jQuery(this).parents('tr');

if (p.next().hasClass('togglerow'))
   p.next().remove();

p.fadeOut(function () {
    jQuery(this).remove();
});

$.ajax({
  URL: "/AdminPanel/News/DeleteNews",
  data: { "newsId": 1 },
  dataType: "json",
  type: "POST",
  success: function (msg) {
  alert(msg);
}
}); 

In this code I get Uncaught TypeError: Cannot read property 'ajax' of undefined.

like image 295
Elvin Mammadov Avatar asked May 11 '14 04:05

Elvin Mammadov


People also ask

How do you fix undefined properties Cannot be read?

The “cannot read property of undefined” error occurs when you attempt to access a property or method of a variable that is undefined . You can fix it by adding an undefined check on the variable before accessing it.

Is not a function Ajax?

ajax is not a function" error occurs when loading the slim version of jQuery and trying to use the ajax function. The ajax function is excluded from the slim jQuery version. To solve the error load the regular jQuery version on your page.


1 Answers

Did you try doing what the rest of the code is doing, using jQuery

jQuery.ajax({
  URL: "/AdminPanel/News/DeleteNews",
  data: { "newsId": 1 },
  dataType: "json",
  type: "POST",
  success: function (msg) {
  alert(msg);
}

You can wrap your code in a DOM ready function that sets the value of $ locally in the function scope, that way you can always use $

jQuery(function($) {
    // code goes here
});
like image 173
adeneo Avatar answered Oct 18 '22 11:10

adeneo