【问题标题】:I got the problem with AJAX request (OctoberCMS)我遇到了 AJAX 请求的问题(OctoberCMS)
【发布时间】:2019-08-29 12:03:27
【问题描述】:

Laravel / 十月CMS $.request 不是函数

我在文件“dist/app.js”(由 webpack 创建)中遇到 AJAX 请求问题。

app.js file:
import 'popper.js';
import 'bootstrap';

$(function () {
$.request('Cart::onAdd', {
'update': {'cart-items': '.cart-item-wrapper'}
});
});

引发错误: jQuery.Deferred 异常:$.request 不是函数类型错误:$.request 不是函数 在 HTML 文档。 (http://localhost/dental/combine/b71d14df8c4d4ddba2c49a7f4750930f-1567079039.js:1232:5) 在mayThrow (http://localhost/dental/combine/b71d14df8c4d4ddba2c49a7f4750930f-1567079039.js:9406:29) 在进程 (http://localhost/dental/combine/b71d14df8c4d4ddba2c49a7f4750930f-1567079039.js:9474:12) 未定义

当我把它放在 html 文件 (layout.htm) 中时

 <script type="text/javascript" src="{{ [
        '@jquery',
        '@framework',
        '@framework.extras',
        'assets/dist/js/app.js',
        ]|theme }}.js">
    </script>
    {% scripts %}
    <script>
        $(function () {
            $.request('Cart::onAdd', {
                'update': {'cart-items': '.cart-item-wrapper'}
            });
        });
    </script>

它工作正常

{% put script %} 也可以正常工作

问题出在webpack生成的文件上

webpack中的jquery很好

【问题讨论】:

    标签: javascript ajax laravel webpack octobercms


    【解决方案1】:

    你不需要先import $ jquery 吗?

    import $ from 'jquery'
    

    如果这不起作用,请尝试将其添加到您的 webpack 配置中:

    externals: {
       jquery: 'jQuery'
    }
    

    【讨论】:

    • webpack.mix.js laravel-mix 我在我的 webpack 中得到了这个:mix.autoload({ jquery: ['$', 'window.jQuery'] }); jquery 在页面的其余部分工作正常
    • @gorny_dev 所以你确定你的代码中的$不是undefined?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    相关资源
    最近更新 更多