Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的javascript返回此错误:$。ajax不是函数_Javascript_Jquery_Ajax_Typeerror - Fatal编程技术网

我的javascript返回此错误:$。ajax不是函数

我的javascript返回此错误:$。ajax不是函数,javascript,jquery,ajax,typeerror,Javascript,Jquery,Ajax,Typeerror,不确定出了什么问题,但我从我的chrome控制台得到了这个错误: 从这个JavaScript: $(function() { //when the DOM is ready var times; //declare global variable $.ajax({ //set up request beforeSend: function (xhr) { //before requesting data if (xhr.overrideMi

不确定出了什么问题,但我从我的chrome控制台得到了这个错误:

从这个JavaScript:

$(function() { //when the DOM is ready
    var times; //declare global variable
    $.ajax({ //set up request
        beforeSend: function (xhr) { //before requesting data
            if (xhr.overrideMimeType) { //if supported
                xhr.overrideMimeType("application/json"); // set MIME to prevent errors
            }
        }
    });
    //funciton that collect data from the json file
    function loadTimetable() { //decalre function
        $.getJSON('data/example.json') //try to collect json data
            .done(function (data) { //if succesful
                times = data; //store in variable
            }).fail(function () { //if a problem: show message
                $('#event').html('Sorry! we couldnt load your time table at the moment');
            });
    }
    loadTimetable(); //call the function

    //CLICK ON TEH EVENT TO LOAD A TIME TABLE
    $('#content').on('click', '#event a', function (e) { //user clicks on place
        e.preventDefault(); //prevent loading page
        var loc = this.id.toUpperCase(); //get value of id attr
        var newContent = "";
        for (var i = 0; i < times[loc].length; i++) { // loop through sessions
            newContent += '<li><span class = "time">' + times[loc][i].time + '</span>';
            newContent += '<a href = "descriptions.html#';
            newContent += times[loc][i].title.replace(/ /g, '-') + '">';
            newContent += times[loc][i].title + '</a></li>';
        }
        $('#sessions').html('<ul>' + newContent + '</ul>'); // Display Time
        $('#event a.current').removeClass('current'); // update selected link
        $(this).addClass('current');
        $('#details').text('');
    });

    //CLICK ON A SESSION TO LEAD THE DESCRIPTION
    $('#content').on('click', '#sessions li a', function (e) { //click on session
        e.preventDefault(); // prevent loading
        var fragment = this.href; //title is in href
        fragment = fragment.replace('#', ' #'); //Add Space before #
        $('#details').load(fragment); //to load info
        $('#sessions a.current').removeClass('current'); //update selected
    });

    //CLICK ON PRIMARY NAVIGATION
    $('nav a').on('click', function (e) { //click on nav
        e.preventDefault(); //prevent loading
        var url = this.href; //get UR: to load
        $('nav a.current').removeClass('current');
        $(this).addClass('current');
        $('#container').remove(); //remove old
        $('#content').load(url + ' #container').hide().fadeIn('slow'); // add new
    });
});
我不确定这是否与我启动.ajax的方式有关,或者我的jquery是否没有正确实现。我想是的。有什么想法吗

编辑:下面是与上面的脚本一起使用的html

<!DOCTYPE html>

<body>
    <header>
        <h1>UseTime</h1>
        <nav>
            <a href="jq-load.html">HOME</a>
            <a href="jq-load.html2">PROFILE</a>
            <a href="jq-load.html4">MANAGE TASKS</a>
            <a href="usetime.html">TIME TABLE</a>
        </nav>
    </header>
    <section id="content">
        <div id="container">
            <div class="third">
                <div id="event">
                    <a id="class1" href="class1.html"><img src="" alt="class1" /> Class 1 </a>
                    <a id="class2" href="class2.html"><img src="" alt="class2" /> Class 2 </a>
                    <a id="class3" href="class3.html"><img src="" alt="class3" /> Class 3 </a>
                </div>
            </div>
            <div class="third">
                <div id="sessions"> Select a Class from the left </div>
            </div>
            <div class="third">
                <div id="details"> Details </div>
            </div>
        </div>
        <!-- container -->
    </section>
    <!-- content -->

    <script src="js/jquery-3.2.1.slim.min.js"></script>
    <script src="js/example.js"></script>
</body>

您正在使用jQuery的精简版本。它不支持ajax调用。 使用


引用自

jQuery 3 slim版本不支持ajax

据报道,

以及jQuery的常规版本,其中包括ajax和 特效模块,我们发布了一个“超薄”版本,不包括这些 模块。总之,它排除了ajax、effects和 不推荐使用的代码

要使用.ajax方法,只需使用完整版本

试试这个jquery-3.2.1.min.js而不是slimjquery-3.2.1.slim.min.js

  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

您是否将jQuery链接到您的代码???链接Jquery​ 在任何其他javascript之前。它可能有用。是的,是的,它是在html中调用的,我会把它放到帖子里。我不知道,这有道理,我会试试看。谢谢你们很多人从这里提到它。这个答案是纯金的。。花了我两个小时。为什么他们要删除AJAX?这是jQuery最好的功能之一。@adhersmnair:Sure-相信大多数应用程序都需要此功能引导示例模板,在“入门”加载slim版本下。必须从他们的CDN中找到合适的。
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
https://code.jquery.com/jquery-3.1.1.slim.js
https://code.jquery.com/jquery-3.1.1.slim.min.js
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>