Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

jquery - '$' is undefined

I get this error whether I put the jQuery $.ajax call in a $(document).ready(function() { or not. This is on a ASP.NET MVC .cshtml file.

 <script type="text/javascript">
    $(document).ready(function() {

        $.ajax({
            url: '/api/courses',
            success: function(data) {
                var list = $('#courses');
                for (var i = 0; i < data.length; i++) {
                    var course = data[i];
                    list.append('<li id="' + course.id + '">' + course.name + '</li>');
                }
            }
        });
    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make sure you reference JQuery first. Something along the lines of (using MS CDN):

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js" type="text/javascript"></script> 
<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            url: '/api/courses',
            success: function(data) {
                var list = $('#courses');
                for (var i = 0; i < data.length; i++) {
                    var course = data[i];
                    list.append('<li id="' + course.id + '">' + course.name + '</li>');
                }
            }
        });
    });
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...