使用ajax调用从servlet到jsp获取arraylist数

使用ajax调用从servlet到jsp获取arraylist数

本文介绍了如何使用ajax调用从servlet到jsp获取arraylist数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在widow.load()事件上通过ajax调用调用了一个servlet ..但是当我想在警告框中显示ajax调用成功后得到的值时,它显示[object XMLDocument]我不知道为什么。这是我第一次使用ajax调用。

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.

这是我的ajax调用代码...`

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)
            }
        });
    });

这是我的servlet代码,我用arraylist formate发送数据。

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您在数据中收到的值是什么,如[mumbai,chennai]?将此值存储在javascript数组变量中。喜欢

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

然后你可以使用jquery .each() 迭代每个城市。

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.

这篇关于如何使用ajax调用从servlet到jsp获取arraylist数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:29