title: Setting Ajax response to drop down using json
body: <p>Here i am using <code>json + ajax + struts2</code>  to create the dependent drop downs. First drop down having some value based on those values second dropdown are loading. In second drop down i am getting response undefined values after every click on the data of the firstdropdown (here i am using <code>onchange</code> event). I am getting the ajax response from struts2  as objects. I want to convert those objects into string for the second dropdown (it should contain id and name). I don't know how to convert objects into string. I tried some of the links in stackoverflow and googled but it doesn't work for me. Here is my code: </p>
  
  <p><strong>Struts2:</strong></p>
  
  <pre><code>            second_drop_downList=retrieveSecond();
  
      ServletActionContext.getResponse().setContentType("text/html");
      PrintWriter out = ServletActionContext.getResponse().getWriter();
      out.print(second_drop_downList);
      out.flush();
  </code></pre>
  
  <p>Here i am getting the correct size how much data is present in database. I am passing those data into the ajax response through above code.</p>
  
  <p>Here is my ajax code:</p>
  
  <pre><code>$(".country").change(function() {
          var id = $(this).val();
          var dataString = 'id=' + id;
  
  
          $.ajax({
              type : "POST",
              url : "sendDropdown.action",
              data : dataString,
              success : function(response) {
                  alert(response);
              var myJSONText = JSON.stringify(response);
              //alert(myJSONText);
              var obj = JSON.parse(myJSONText);
              alert(obj);
              var html = '&lt;option value=""&gt;select Section&lt;/option&gt;';
              for ( var i = 0; i &lt; obj.length; i++) {
              html += '&lt;option value="'+obj[i].id+'"&gt;' + obj[i].dep_name
              + '&lt;/option&gt;';
              }
              html += '&lt;/option&gt;';
  
              $('.city').html(html); 
              }
          });
  
      });
  </code></pre>
  
  <p>Here i am getting the response as hexadecimal format(in alert).But in dropdown i am getting undefined.</p>