我的代码有什么错误?

I'm using smarty template and php. The following code is being written in smarty template.

{literal}
<script type="text/javascript">
// This function gets test when category checkbox is checked

function get_subjects_by_class(class_id) {
    var field_id = 'subjects';
    $.ajax({
        url: "teacher_details.php",
        type: "GET",
        data: {
            'request_type': 'ajax',
            'op': 'get_assigned_subject_list',
            'class_id': class_id
        },  
        success: function (data) {
            $('#category_test_container').append(data);
        });
    }
</script>
{/literal}

Call to this function is as follows :

<a href="#" onClick="get_subjects_by_class({$class.class_id}); return false;">{$class.class_name}</a>

Upon clicking on hyperlink I'm getting an error as follows :

ReferenceError: $ is not defined
$.ajax({

I googled it for the error resolution, but couldn't get the desired resolution. Can anyone help me out to resolve this error? Thanks in Advance.

You either haven't loaded jQuery or you've loaded it after you've run this script. This is why $ is undefined.

You need to include jQuery first before the $ variable becomes available. $ is the short for jQuery, so obviously jQuery isnt included at the point where $.ajax is run.

it seems that jquery is not included on the page. Please include jquery from any CDN

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>

It can happen due to conflicts also for that case use

http://api.jquery.com/jQuery.noConflict/