0

i'm doing in CQ5, a component that connect with a servlet and get this info:

Output Servlet (json format)= [{"text":"A","value":10},{"text":"B","value":20}]

for show A and B in a drop down menu.

This my html code:

<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:

<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. Maybe I have an error of syntax in my script code. How can I solve?

6
  • its true but uncaught typeerror undefined is not a function is the problem now </strong> was an error typing the question :I Commented Apr 1, 2015 at 8:52
  • Okay... could have been then answer Commented Apr 1, 2015 at 8:54
  • Your debugger should tell you which line the error is on. Commented Apr 1, 2015 at 8:54
  • You are using an id for your select options, should be class since it is not unique. Commented Apr 1, 2015 at 8:56
  • before error:function(){ in " }, " in this place i get Uncaught typeerror undefined is not a function according debugger. Commented Apr 1, 2015 at 9:00

2 Answers 2

1

There are a conflict between 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>');
            }
    });
})
Sign up to request clarification or add additional context in comments.

Comments

0

Your code appears to work, only problem I found was that you are not specifying a request type in your AJAX call.

Just add: type: 'post',

Here is a JSFiddle.

3 Comments

yep this is the next step now but its work now. i fix it as i writed in my post, thanks!.
Sorry the fiddle wasn't working, forgot to update it :)
read my post where explain there are a conflict between 2 jQuery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.