【问题标题】:How to retrieve the value sent from struts action in javascript by parsing json data如何通过解析json数据检索从javascript中的struts动作发送的值
【发布时间】:2014-12-19 06:20:57
【问题描述】:

我是新手。我正在使用 httpservlet 响应从 struts 操作发送数据。我应该怎么做才能在 javascript 中检索此值以将这些值设置为 html 元素。

使用 Struts 动作和 httpservlet 对象写入响应。

Gson gson = new Gson(); //using gson-2.3.1.jar 
//log.debug("Printing values ... ");
//log.debug(gson.toJson( languagesResponse.getLanguages()));

response.setContentType("application/json");
// Get the printwriter object from response to write the required json object to the output stream      
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object  
out.print(gson.toJson( languagesResponse.getLanguages()));
out.flush();

让json返回值为:

[{"stringCode":"default","numberCode":0,"__hashCodeCalc":false},{"stringCode":"en_US","numberCode":1,"__hashCodeCalc":false}]

我想为 javascript elemtent 获取这个值。

【问题讨论】:

    标签: javascript jquery json gson struts-1


    【解决方案1】:

    您如何从 HTTP 服务器响应与此问题无关,因为您要问的是客户端代码。这也可以看作是多个问题的重复,您可以从中获得答案,例如this(对这个想法有很好的概述)。您还可以查看 jQuery 的这两部分以获取实际代码示例:AJAX & DOM manipulation

    【讨论】:

    • 我知道要处理它的客户端代码,但我不知道我应该如何在客户端获取响应数据..
    • 您查看我提供的链接了吗?那些应该有关于处理所有你问的相当广泛的材料。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2013-12-23
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多