【问题标题】:$.ajax not defined - JavaScript and HTML$.ajax 未定义 - JavaScript 和 HTML
【发布时间】:2016-11-30 06:28:30
【问题描述】:

我在使用 ajax 时遇到问题。

我有这个 JavaScript 代码:

<script type="text/javascript">
function updateDepartment()
{
     $.ajax({
         type: "GET",
         url: "getSubSubjectsList",
         data:{
            subjectName: "",
          },
          headers: {
              Accept: 'application/json'
             },
          dataType: 'json',
          contentType:"application/x-javascript; charset:ISO-8859-1",



          success: function (response) {


           var subSubjectsList =  $.parseJSON(JSON.stringify(response));
           window.alert(JSON.stringify(response));
           var select = document.getElementById("sub_subjects_select");
                 for (var i = 0; i < subSubjectsList.length; i++) {
                     var option = document.createElement("option");
                     option.text =subSubjectsList[i].description;
                     option.value = subSubjectsList[i].subjectObj.view;
                    //window.alert(subSubject.description +"  "+subSubject.id+"  "+subSubject.subjectObj.id+"  "+subSubject.subjectObj.view+"  "+ subSubject.departmentObj );


                      select.add(option);
                    }
                 openDialog('changeDepartment');
                 },
           //$("#subSubjectDiv").html( response ); call to another page .jsp

        error: function(xhr,textStatus,err) { window.alert(JSON.stringify(xhr) + " ... "+textStatus+" ... "+err);}
     });





}


</script>

当我运行它时,我得到了这个错误:

editContact?contacting_id=8:392 Uncaught ReferenceError: $ is not defined updateDepartment @ editContact?contacting_id=8:392onchange @ editContact?contacting_id=8:251

我的控制器中有名为“getSubSubjectsList”的函数。

这是我的控制器功能:

@RequestMapping(value = "/getSubSubjectsList", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody  List<sub_subject> getSubSubjectsList(@RequestParam("subjectName") String subjectName) {


    subSubjectsList= Sub_SubjectDAO.getSubSubjectsList(language);
    return subSubjectsList;
}

我需要为这个问题做些什么?

【问题讨论】:

  • 您在运行脚本之前加载了 jquery 吗?

标签: javascript html ajax spring-mvc


【解决方案1】:

这就是答案。我忘了在我的 Html 代码中添加这个脚本:

&lt;script src="code.jquery.com/jquery-1.7.1.min.js"&gt;&lt;/script&gt;

加载jquery很重要!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多