简体   繁体   English

requirejs require jQuery未定义

[英]requirejs require jquery is undefined

I have read many SO questions about this and I even moved out the config from data-main attribute as suggested by one of the answers, still no luck. 我已经阅读了很多有关此的问题,我甚至按照答案之一的建议从data-main属性中移出了配置,仍然没有运气。

I have rj_config.js 我有rj_config.js

"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 project.js 和project.js

"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);
        }
    });
});

HTML 的HTML

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

I am still receiving error Uncaught TypeError: Cannot read property 'ajaxSetup' of undefined in project.js 我仍然收到错误Uncaught TypeError: Cannot read property 'ajaxSetup' of undefined project.js Uncaught TypeError: Cannot read property 'ajaxSetup' of undefined

Came across an answer which indirectly hinted something that I should use requirejs instead of require , since I already defined require in rj_config.js 跨越一个答案间接暗示的东西,我应该用来到requirejs ,而不是require ,因为我已经定义require在rj_config.js

So after changing to requirejs and worked UPDATE: works sometimes: 因此,在更改为requirejs并运行UPDATE之后:有时可以工作:

"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);
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM