【问题标题】:Uncaught SyntaxError: Unexpected token (Script Ajax Call)Uncaught SyntaxError: Unexpected token (Script Ajax Call)
【发布时间】:2015-04-01 08:47:13
【问题描述】:

我正在使用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。也许我的脚本代码中有语法错误。我该如何解决?

【问题讨论】:

  • 其真实但未捕获的类型错误 undefined is not a function is the problem now 是输入问题时出错:I
  • 好吧...本来可以回答
  • 你的调试器应该告诉你错误在哪一行。
  • 您正在为您的选择选项使用 id,应该是 class,因为它不是唯一的。
  • before error:function(){ in " }," 在这个地方我得到 Uncaught typeerror undefined is not a function 根据调试器。

标签: javascript jquery ajax servlets aem


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    只需添加:type: 'post',

    这是JSFiddle

    【讨论】:

    • 是的,这是现在的下一步,但它现在可以工作了。我按照我在帖子中写的那样修复它,谢谢!。
    • 抱歉小提琴没有用,忘记更新了:)
    • 阅读我的帖子,解释 2 jQuery 之间存在冲突
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    相关资源
    最近更新 更多