简体   繁体   English

未捕获的SyntaxError:意外令牌(脚本Ajax调用)

[英]Uncaught SyntaxError: Unexpected token (Script Ajax Call)

i'm doing in CQ5 , a component that connect with a servlet and get this info: 我正在做CQ5 ,一个与servlet连接并获取此信息的组件:

Output Servlet (json format)= [{"text":"A","value":10},{"text":"B","value":20}] 输出Servlet(json格式)= [{{text“:” A“,” value“:10},{” text“:” B“,” value“:20}]

for show A and B in a drop down menu. 在下拉菜单中显示A和B。

This my html code: 这是我的html代码:

<div>
   <form action="/bin/company/repo" method="post">
        <select id="options">
        </select>
    <input type="submit" id="send" value="Send">  
    </form>
    <p id="demo"></p>
</div>

For insert the options (select), i do this javascript in the jsp of component: 对于插入选项(选择),我在组件的jsp中执行以下javascript:

<script type="text/javascript">
    //get a reference to the select element
    $select = $('#options');
    //request the JSON data and parse into the select element
    $.ajax({
      url: '/bin/company/repo',
      dataType:'JSON',
      success:function(data){
        //clear the current content of the select
        $select.html('');
        //iterate over the data and append a select option
        $.each(data, function(text, value){
          $select.append('<option id="' + value.value + '">' + value.text + '</option>');
        });
      },
      error:function(){
        //if there is an error append a 'none available' option
        $select.html('<option id="-1">none available</option>');
      }
    });
</script>

But I get Uncaught typeerror undefined is not a function . 但是我得到Uncaught typeerror undefined is not a function Maybe I have an error of syntax in my script code. 也许我的脚本代码中有语法错误。 How can I solve? 我该如何解决?

There are a conflict between 2 jQuery: 2个jQuery之间存在冲突:

We can delete one or modify the code like this: 我们可以删除一个或修改如下代码:

<script type="text/javascript">
var j = jQuery.noConflict();
j(document).ready(function(){
    //get a reference to the select element
    //request the JSON data and parse into the select element
    j.ajax({
            url: '/bin/company/repo',
            dataType:'JSON',
            success:function(data){
              //clear the current content of the select
              j('#abcd').html('');
              //iterate over the data and append a select option
              jQuery.each(data, function(text, value){
                 j('#abcd').append('<option id="' + value.value + '">' + value.text + '</option>');
              });
            },
            error:function(){
               //if there is an error append a 'none available' option
               j('#abcd').html('<option id="-1">none available</option>');
            }
    });
})

Your code appears to work, only problem I found was that you are not specifying a request type in your AJAX call. 您的代码似乎可以正常工作,我发现的唯一问题是您没有在AJAX调用中指定请求类型。

Just add: type: 'post', 只需添加: type: 'post',

Here is a JSFiddle . 这是一个JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM