Sam Gentile
Sam Gentile

Reputation: 1349

'$' 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>');
                }
            }
        });
    });

Upvotes: 1

Views: 486

Answers (2)

Papa Stahl
Papa Stahl

Reputation: 887

On a server on the local network with IE 11, it can also be a compatibility view issue.

See my answer to this question for more details: '$' is undefined. How to use jQuery 2.0.1 in empty ASP.NET MVC 4 project?

Upvotes: 0

Sheridan Bulger
Sheridan Bulger

Reputation: 1214

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>

Upvotes: 7

Related Questions