首页 > 解决方案 > 浏览器控制台中的 ajax 错误 - 不是函数

问题描述

我不明白为什么我在浏览器控制台中看到它是一个错误。
Uncaught TypeError: $.ajax is not a function - 我看到这条消息

在此处输入图像描述

我只发现它不是库的正确版本的建议。但我认为我的版本是正确的。
我的代码

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.js"></script>
<input type="button" id="id_name_task" value="Click" />


<script type="text/javascript">
  $(document).ready(function() {
    $("#id_name_task").click(function() {
      var name_task = $(this).val();
      console.log(name_task);
      $.ajax({
        type: 'GET',
        async: true,
        dataType: 'json',
        url: '/validate_data/',
        data: {
          'name_task': name_task
        },
        success: function(data) {
          if (data.is_taken) {
            alert("A task with this name already exists.");
          }

        },
      });
    });
  });
</script>

标签: jqueryajaxfrontend

解决方案


查看代码我发现 jquery.js 和 jquery.slim.js 之间存在以下差异:

在 jquery.slim.js 中,去掉了以下代码功能:

  1. jQuery.fn.extend
  2. jquery.fn.load
  3. jquery.each // 附上一堆处理常见AJAX事件的函数
  4. jQuery.expr.filters.animated
  5. ajax 设置,如 jQuery.ajaxSettings.xhr、jQuery.ajaxPrefilter、jQuery.ajaxSetup、jQuery.ajaxPrefilter、jQuery.ajaxTransport、jQuery.ajaxSetup
  6. 像 jQuery.parseXML 这样的 xml 解析,
  7. jQuery.easing、jQuery.Animation、jQuery.speed 等动画效果

jquery的普通包和slim包有什么区别?

$.ajax 从 jQuery slim 3.2.1 中移除


推荐阅读