简体   繁体   English

如何返回JSON对象并捕获HTML中的Ajax调用

[英]How to return an json Object and catch in in Ajax call in HTML

I have a simple tree structure of information in my D3 code. 我的D3代码中有一个简单的信息树结构。 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. D3脚本从脚本d3.json()函数调用json,结果json提供了树结构数据。现在,我有一些信息将来自数据库并访问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.. 我必须在Servlet中动态制作json,以便它可以根据用户响应进行更改。这是我使用的脚本和json。

This is my HTML file from where I made an Ajax call to the Servlet. 这是我对Servlet进行Ajax调用的HTML文件。 The Ajax is calling the Servlet ,but there is an error in getting back the response. Ajax正在调用Servlet,但是返回响应时出错。 I am getting an "error occurred" message.When I am running the servlet...... 我收到“发生错误”消息。运行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>

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 ... 这是我试图从中获取响应的HTML文件,只是调用Servlet。但是在响应中,我收到一条错误事件消息...

IN the accountServlet i am creating the json like this 在accountServlet中,我正在像这样创建json

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

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对象并将响应返回到脚本的信息吗?请任何人帮助

You have to create json Object in the servlet. 您必须在servlet中创建json对象。 And then retrive that json using ajax call in your script. 然后在脚本中使用ajax调用检索该json。 For creating json in servlet you can use any library like Gson or java-json . 为了在servlet中创建json,您可以使用Gsonjava-json之类的任何库。

For example: 例如:

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

And the ajax code as below: 而ajax代码如下:

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

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM