Showing 1 – 1 of 1 replies
cooliyev

How to use jquery ajax requests in custom js file?

cooliyev PURCHASED
1 year ago
Hello, I'm using Smatry v.5 and Laravel 8. And trying to use ajax in custom.js but i have a "Jquery is not defined" in the console.
How to use jquery ajax requests in custom js file?

Also i have tried to use data-ajax-url="{{route('route-name')}}" like in /html_frontend/documentation/plugins-sow-ajax-content.html but console tells me - no valid link..
<a href="#" class="js-ajax" data-ajax-url="{{route('route-name')}}">Link</a> and want the response to appear in div#category

Could you write an example how to properly use ajax requests ?
stepofweb
stepofweb SELLER
1 year ago
Hello cooliyev,

To access jquery, you have to load Smarty javascript files this way (in this order, without async tag):

<script src="assets/js/core.min.js"></script>
<script src="assets/js/vendor_bundle.min.js"></script>

<script src="custom.js"></script>

Now you can access anything (jquery, plugins, bootstrap, etc).
jQuery is packed inside core.min.js
Bootstrap is packed inside vendor_bundle.min.js

About loading ajax content - you get the error because the URL is not valid for some reason.
I am not sure how you use "{{route('route-name')}}" but this must be a valid link and Laravel response should have 200 header status (404 header status will trigger an error - this is how of jQuery ajax works).