Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In laravel blade page saying $.ajax is not a function

In laravel I used to call ajax for my select box(it is like binary tree, ex: cat1,cat2 are parent id then cat3,cat4,cat5 are childs of parent cat1, cat6,cat7 are childs of cat2 and so on..). Onchange function is also working, but showing ajax error Uncaught TypeError: $.ajax is not a function

This is my script

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<meta name="csrf-token" content="{{ csrf_token() }}" />
<script>
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
function fun_end_cat(cat_id)
{
//alert("test");
    var cat=cat_id.value;
    $.ajax({
           url: '/postajax',
           type: 'POST',
           data: {_token: CSRF_TOKEN, message:cat},
           dataType: 'JSON',
           success: function (data) { 
                 alert(data);
             }
           }); 
} 
</script>

this my select box

<select class="form-control" name="cat_name" required onchange="fun_end_cat(this)">
    <option value="">Select Category</option>
    <option value="cat1">cat1</option>
    <option value="cat1">cat1</option>
</select>
like image 571
Varadegowda V Avatar asked Jul 18 '19 06:07

Varadegowda V


People also ask

Why is ajax not a function?

AJAX is not a programming language. It requires a built-in browser with the XMLHttpRequest object that requests data from the server, and it uses JavaScript and HTML DOM to display data.

How we can use ajax in laravel?

The ajax request can be GET or POST or other valid types. To use AJAX in Laravel, you need to import a jquery library in your view file to use ajax functions of jquery, which will be used to send and receive data using ajax from the server.

Where do I put ajax code in laravel?

How to use ajax in Laravel? $. ajax({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]'). attr('content') }, url : "{{ route('saveToken') }}", data : {'token' : token}, type : 'POST', dataType : 'json', success : function(result){ console.


1 Answers

You can use it like this, $ sign might be conflicting with other libs you are using on the page.

$.noConflict(); // it resolves $ issues so that other libraries using $ work seamlessly

jQuery.ajax(function(){
    // use jQuery instead of $
});
like image 53
Farooq Ahmed Khan Avatar answered Oct 11 '22 16:10

Farooq Ahmed Khan