簡體   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