如何返回JSON对象并捕获HTML中的Ajax调用 [英] How to return an json Object and catch in in Ajax call in HTML

查看:105
本文介绍了如何返回JSON对象并捕获HTML中的Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的D3代码中有一个简单的信息树结构. D3脚本从脚本d3.json()函数调用json,结果json提供了树结构数据.现在,我有一些信息将来自数据库并访问Servlet.我必须在Servlet中动态制作json,以便它可以根据用户响应进行更改.以下是脚本&我使用的json.

I have a simple tree structure of information in my D3 code. The D3 script is calling json from the script d3.json() function and the resulting json is giving the tree structure data.Now I have a couple of information that will come from database and hit the Servlet. I have to make the json dynamically in the Servlet so that it can change upon user response.Here is the script & the json I used..

这是我的HTML文件,在该文件中我对Servlet进行了Ajax调用. Ajax正在调用Servlet ,但取回响应时出错.我收到发生错误"消息.当我运行servlet时……

This is my HTML file from where I made an Ajax call to the Servlet. The Ajax is calling the Servlet ,but there is an error in getting back the response. I am getting an "error occurred" message.When I am running the servlet......

 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>            
        function doajax(){
            $.ajax({
                url: "AccountServlet",
                type: "post",
                dataType: "json",

                error:function(){
                    alert("error occured!!!");
                },
                success:function(data){
                    alert(data.fullName + "\n" + data.mobileNo);
                }
            });
        }
    </script>

这是我正在努力获取回复的HTML 这个文件只是调用一个Servlet.但是在响应中,我收到一条错误事件消息...

This is the Html from where i am tring to get the response back this file just calls a Servlet.But in the response i am getting a error occuree message ...

在accountServlet中,我正在像这样创建json

IN the accountServlet i am creating the json like this

     ArrayList<DistinctSourceCount> countList = new ArrayList<DistinctSourceCount>();
        countList.add(new DistinctSourceCount("Jan", 1800));
        countList.add(new DistinctSourceCount("Feb", 1500));
        countList.add(new DistinctSourceCount("March", 2000));
        countList.add(new DistinctSourceCount("April", 1550));
        countList.add(new DistinctSourceCount("May", 1000));
        countList.add(new DistinctSourceCount("June", 1700));
        countList.add(new DistinctSourceCount("July", 1400));
        countList.add(new DistinctSourceCount("Aug", 1900));
        countList.add(new DistinctSourceCount("Sept", 1000));
        countList.add(new DistinctSourceCount("Oct", 1500));
        countList.add(new DistinctSourceCount("Nov", 1100));
        countList.add(new DistinctSourceCount("Dec", 2000));

        Gson gson = new Gson();
        JsonArray arrayObj = new JsonArray();
        for (int i = 0; i < countList.size(); i++) {
            DistinctSourceCount count = countList.get(i);
            JsonElement linObj = gson.toJsonTree(count);
            arrayObj.add(linObj);
        }

        JsonObject myObj = new JsonObject();
        myObj.addProperty("success", true);
        myObj.add("topList", arrayObj);
        myObj.addProperty("totalCount", countList.size());

        System.out.println(myObj.toString());
        System.out.close();

这是我到目前为止已完成的代码.任何人都可以帮助我有关如何在Servlet中创建json对象以及如何将响应返回到脚本的信息吗?请任何人帮助

This is my code I have done so far.Can anyone help me about how to create the json object in Servlet and getting the response back the script??please anyone help

推荐答案

您必须在servlet中创建json对象.然后在脚本中使用ajax调用检索该json.要在servlet中创建json,您可以使用任何库,例如 Gson

You have to create json Object in the servlet. And then retrive that json using ajax call in your script. For creating json in servlet you can use any library like Gson or java-json.

例如:

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","flare");
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject_child = new JSONObject();
        jsonObject_child.put("name", "analytics");
        jsonArray.put(jsonObject_child);
        jsonObject.put("children",jsonArray);
        System.out.println(jsonObject);

以及如下的ajax代码:

And the ajax code as below:

        $.ajax({
        type: "POST",
        url: "PATH_OF_SERVLET",
        dataType: 'json',
        success: function(response) {
        // Parse your response from Servlet here.
        }

    });

这篇关于如何返回JSON对象并捕获HTML中的Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆