jquery - 将 JSON 从操作类传递到 JSP,而不在 struts 中使用 Ajax

标签 jquery ajax json jsp struts

在 JSP 中,我可以使用下面的代码从操作类获取 JSON。有人可以告诉我是否有一种方法可以在不使用 Ajax 的情况下从操作类接收响应(JSON)?

$.ajax({
url:"/StrutsExample1/helloWorld.do",
data:params,
type: "post",
dataType:"json",
success:function(response){
          $.each(response,function(i){  
              $.each(response[i],function(key,value){
                 $('#info').html(key+" "+value);})
           })
},
    error:  function(e){ alert("fail " + e);}                   
})

更新:

以下代码用于操作类中。

response.setContentType("text/json; charset=utf-8");
JSONObject json = createJsonObject();  //to construct the JSON
PrintWriter out = response.getWriter();
out.print(json);
out.flush();
return mapping.findForward("success");

在 struts-config.xml 中,我在操作映射中有以下内容:

<action path="/helloWorld" type="test.action.HelloWorldAction" name="helloWorldForm">
<forward name="success" path="/index.jsp" />

然后,在 JSP 文件中,而不是像上面那样使用 Ajax 调用,它看起来像:

$(function(response){
   $.each(response,function(i){ 
              $.each(response[i],function(key,value){
                 $('#info').html(key+" "+value);})
           })
})

但是,执行时键和值未定义。 JSON 在操作类中正确构造。请帮忙指出如何将JSON传递给jsp。

最佳答案

是的。你可以用正常的方式做。通过提交页面,您可以到达操作类并将您的响应设置为

response.setContentType("application/json");

关于jquery - 将 JSON 从操作类传递到 JSP,而不在 struts 中使用 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19666908/

相关文章:

javascript - 如何创建始终打开到固定点的圆弧

jquery - 关于使用 Webmatrix 和 AJAX 的基本问题

javascript - 路由更改时更改元标记

jquery - Rails 4 使用 ajax、jquery、:remote => true, 和 respond_to 渲染部分

javascript - 显示预览文件

javascript - 将 jQuery JSON 中的特定数据获取到变量中

javascript - 分割和聚合 json 数据

json - Haskell Aeson 解析混合元素数组

javascript - 动态 css 文件和 javascript

javascript - 根据解析的 XML 纬度和经度值将 Google map 居中