繁体   English   中英

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

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

我正在做CQ5 ,一个与servlet连接并获取此信息的组件:

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

在下拉菜单中显示A和B。

这是我的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>

对于插入选项(选择),我在组件的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>

但是我得到Uncaught typeerror undefined is not a function 也许我的脚本代码中有语法错误。 我该如何解决?

2个jQuery之间存在冲突:

我们可以删除一个或修改如下代码:

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

您的代码似乎可以正常工作,我发现的唯一问题是您没有在AJAX调用中指定请求类型。

只需添加: type: 'post',

这是一个JSFiddle

暂无
暂无

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

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