3

I called a servlet through ajax call on widow.load() event ..But when i want to show the value got after success of ajax call in alert box it is showing [object XMLDocument] i dont know why .this is the first time i am using ajax call.

Here is my ajax call code...`

$(window).load(function() {
        $.ajax({
            type: 'GET',
            url: 'Sites',
            datatype:'text',
            success: function(data) {
                alert(data);
                debugger;
                var city=data;
                for(var i in city)
                {
                    output ='<input type="checkbox"   id="'+city[i]+'" name="'+city[i]+'" value="'+city[i]+'" />'+city[i]+'<br />'
                }
                console.log(output)
            }
        });
    });

And here is my servlet code from where i sending data in arraylist formate.

PrintWriter out = response.getWriter();
    ArrayList calltype = new ArrayList();

    try {
        String strQuery = "";
        ResultSet rs = null;

        conexion conexiondb = new conexion();
        conexiondb.Conectar();

        strQuery = "Select * from sites";

        rs = conexiondb.Consulta(strQuery);

        while (rs.next()) {
            String toc = rs.getString("sites");
            calltype.add(toc);
        }

        out.print(calltype);
        System.out.println(calltype);
        out.close();

    } catch (Exception e) {
        // display stack trace in the browser
        System.out.println(e);
    }

Any help on this will be appreciated .. Thanks in advance..

Adi
  • 1,395
  • 11
  • 37
  • 61
  • 1
    Are you sure your ajax call going to servlet? – Vinoth Krishnan Oct 29 '13 at 13:03
  • @VinothKrishnan yes it is going to servlet ..from firebug firebug status its OK and response is also coming for site name like [Mumbai, Delhi, Bangalore] – Adi Oct 29 '13 at 13:08
  • make your `dataType : "text"` or `dataType : "html"`. This will returns text. For more information read [jQuery Ajax Article](http://api.jquery.com/jQuery.ajax/) – Vinoth Krishnan Oct 29 '13 at 13:21
  • @VinothKrishnan thank you very much sir now values of city is showing in alertbox but it is not going inside the loop to dynamically make checkbox please check my script code.. – Adi Oct 29 '13 at 13:55
  • @Adi What does it alret? – fujy Oct 29 '13 at 14:39

1 Answers1

2

@Adi what are the values you are receiving in data, like [mumbai,chennai]? Store this values in javascript array variable. Like

var values = [];
values = data;

Then you can use jquery .each() jQuery each function to iterate through each cities.

$.each(values, function( index, value ) {
   alert( index + ": " + value );
});

I haven't checked this code. Please let me know if this helps.

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34