Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get arraylist data from servlet to jsp using ajax call [duplicate]

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

like image 332
Adi Avatar asked Dec 06 '25 03:12

Adi


1 Answers

@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.

like image 164
Vinoth Krishnan Avatar answered Dec 07 '25 17:12

Vinoth Krishnan