The absence of jQuery in the required requirejs

I've gone through several Stack Overflow questions regarding this issue, and I even tried moving the config from the data-main attribute as suggested in one of the answers, but unfortunately, it didn't work for me.

Here is my rj_config.js file:

"use strict";

var require = {
    baseUrl: '/static/js',
    paths: {
        'jquery': '../vendor/jquery/dist/jquery.min',
        'jquery-ui': '../vendor/jquery-ui',
        'bootstrap': '../vendor/bootstrap-sass/assets/javascripts/bootstrap.min'
    },
    shim : {
        'bootstrap' : { 'deps' :['jquery'] }
    }
};

And here is my project.js file:

"use strict";

require(['jquery', 'bootstrap'], function($, bootstrap) {
    // sets up the csrf on ajax calls
    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            var csrftoken = $('meta[name="csrf-token"]').attr('content');
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    });
});

Regarding the HTML code:

<script src="{% static 'js/rj_config.js' %}"></script>
<script data-main="project" src="{% static 'vendor/requirejs/require.js' %}"></script>

Despite implementing these changes, I'm still encountering the error message "

Uncaught TypeError: Cannot read property 'ajaxSetup' of undefined
" in the project.js file.

Answer №1

While browsing through an answer, I came across a suggestion to use requirejs instead of require. This advice stemmed from the fact that I had already defined require in rj_config.js.

After making the switch to requirejs, I noticed some improvements although there are occasional issues:

"use strict";

requirejs(['jquery', 'bootstrap'], function($, bootstrap) {
    // sets up the csrf on ajax calls
    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            var csrftoken = $('meta[name="csrf-token"]').attr('content');
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The "next" button on my carousel is unresponsive and the automatic transitioning feature is not working

I'm having trouble implementing a carousel on my website. The next/previous buttons and automatic slides are not functioning properly. <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <!- ...

How to Use Conditional In-Line CSS for Internet Explorer and Other Browsers

After inspecting the source code for this website, I noticed a cool feature where the page changes based on your scrolling position. It creates a visually appealing effect. Upon further examination of the source in FireFox, I observed the use of -webkit-t ...

Incorporate a PHP file using AJAX in a development project with Vue

After utilizing vue-cli for constructing a Vue project, I encountered an issue with one of its scripts in the package.json file: "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot", I rely on this script for my project development. Ho ...

Looking to dynamically generate HTML tags using jQuery and JSON?

Looking for help with inserting HTML code into a div using jQuery. <div id="addme"></div> Here is some HTML with PHP: <div class="col-md-4 product secondproduct"> <div class="images1"> <a href="<?php echo base_u ...

extract the ID from an array and switch its state

I'm currently working on developing an "auto toggle" feature. My initial idea was to use a loop for this purpose. The array[i].onclick part is functioning as expected, but I am facing challenges in implementing the toggle animation. function setConte ...

Tips for concealing an <li> element when the Button features the text "sometext" using jQuery

I have a table displayed in the image above that I need to hide using jQuery when the button inside it is labeled "ASK". Although I know how to make the table disappear with the code $("div.table").css("display", "none");, I am having trouble relating the ...

JavaScript: Populating an Array with Image URLs Using a Loop

My image gallery project has hit a roadblock and my JavaScript skills are not up to par. Maybe it's time to brush up on the basics with a good book from the library. Here's what I want to achieve: 1. Create an array of URL's for each imag ...

Vue.js experiencing issue with rendering Three.js library

When I began my project with vue init webpack my-project, I found myself focusing on three key files: main.js, AudioPlayer.vue, and App.vue. Despite not encountering any errors, I couldn't figure out why the three.js code was not rendering in the App. ...

Confusion arising from the combination of jQuery and Ajax

Being a new user, I am seeking assistance with JQuery and Ajax as my proficiency lies in PHP only. The issue is related to a newsletter signup section on an HTML page: <h4>Newsletter</h4> <form id="main-news-contact-form" class="news-cont ...

Implementing Django AJAX form submission: extracting a targeted model field from the form

In order to achieve my project goals, I am looking to implement a form submission using Ajax without any page refreshing. The Post model I am working with contains three fields: animal, image, and description. Here is an image of the model class Post(mode ...

Tips for handling a nested function in a redux action unit test

Currently, I am diving into the world of unit testing to test the functionalities of our react application, particularly focusing on actions. In my initial attempts, I encountered a scenario where I have a function that returns an object with a nested fun ...

Applying a CSS transition to transform properties excluding rotation effects

I'm looking to apply a transitional animation to an element's transform property without affecting the rotation. Here is the code snippet: <html> <head> <link rel="stylesheet" href="style.css"> <script src="script.js ...

Is it possible for Vue Router to remember the scroll position on a route and return to the same position when navigating back?

My Vue Router is not saving the scroll position and always loads at the top of the page (0, 0). Any suggestions on what could be causing this issue? Here is my current router code setup: const scrollBehavior = (to, from, savedPosition) => { if (saved ...

The presence of attribute binding and the ng directive results in a webpage that fails to render

Currently on my Angular learning journey, I am following the instructions and guidance provided in this helpful tutorial. However, I encountered a problem while running the command: ng serve --open Instead of seeing the expected items displayed on http:/ ...

Upon clicking the "show more information" button on the page, JavaScript executes the function to display additional details

<a href="#" class="button">Read More</a><br> I'm currently struggling with programming a button on my website. Specifically, I need the Read More button to display all information related to the CDs I have for sale when clicked. A ...

The Viadeo Social Toolbox seems to be encountering technical difficulties at the moment

I attempted to utilize a Viadeo Social Toolbox, specifically the Viadeo Share Button, but it seems to be malfunctioning in certain browsers? I came across some outdated viadeo share URLs like this: http://www.viadeo.com/shareit/share/?url=${url}&title ...

Issue with closing in JavaScript code not functioning as expected

I recently created a toggle bar using HTML/CSS and I attempted to implement JavaScript code to close it. However, despite my efforts, the script doesn't seem to be functioning properly. Code: $(document).ready(function(){ $(".fa-times").click(functi ...

Toggle visibility of column in table using cookies

I was looking to store user preferences for shown/hidden columns in the browser using cookies, so that the table layout wouldn't reset upon page refresh. Fiddle : http://jsfiddle.net/HvA4s/72/ HTML <a href="edit" id=edit>Show/Hide Columns< ...

Countdown clock facing a date malfunction in JavaScript

After spending some time writing code to create a countdown clock for my website, I've run into an unexpected issue. Here is the HTML snippet: <div id="deadline_Container"> <div id="clockdiv"> <span class="deadline_Text">Time rem ...

Issue with useState hook: Array value not initializing correctly

When passing an array called 'defaults' into my panel component and attempting to set the state hook 'selected option' equal to it, the hook ends up setting an empty array instead of the desired values. Here is a snippet of the compone ...