【问题标题】:Sending JSON data to next jsp page将 JSON 数据发送到下一个 jsp 页面
【发布时间】:2014-04-22 12:48:50
【问题描述】:

我的问题如下: 我正在对 jsp 进行 AJAX 调用,并从他们那里得到一个包含标志和 ArrayList 的 JSON 对象

JSP:

Gson gson = new Gson();
JsonObject root = new JsonObject();
root.addProperty("flag", flag); //add flag
root.addProperty("list", gson.toJson(list)); 
out.println(gson.toJson(root));

现在在我的 AJAX 函数的成功函数中,我想移动到其他一些 jsp,所以我做了这样的事情:

success: function(response){
alert(JSON.stringify(response.list));
//alert(JSON.stringify(response.flag));
if(JSON.stringify(response.flag).indexOf("true")>=0){
    location.href="GroupLoginScreen.jsp";  
}
else{
    alert("UNSUCCESSFUL");
}
}

现在,问题部分是虽然我在这里得到了正确的结果但是如何使用 GroupLoginScreen.jsp 将此列表作为 URL 中的参数发送,以便在接收端可以通过执行以下操作来获得它:

ArrayList<String> list1 = (ArrayList<String>)request.getAttribute("mylist");

【问题讨论】:

  • 当你说它不起作用时,给出的错误是什么?
  • @RoryMcCrossan 我无法通过警报移动到下一页(response.flag); alert(response.list);提供正确的结果。另外我不知道如何将此列表与 url 作为参数传递给下一页以及如何在接收端获取它

标签: javascript jquery ajax json jsp


【解决方案1】:

location.href="GroupLoginScreen.jsp" 将从浏览器发出新的 GET 请求,因此在 GroupLoginScreen.jsp 中,您无法像以前那样获取 'mylist' 属性。

您的解决方案是您必须使用 encodeURIComponent 函数将列表编码为字符串,然后将“mylist”作为查询参数添加到 GroupLoginScreen.jsp。

代码是这样的:

var mylistParam = encodeURIComponent ( json_text_of_the_list );

location.href = "GroupLoginScreen.jsp?mylist=" + mylistParam;  

在 GroupLoginScreen.jsp 中。

String myList = request.getParameter("mylist");

Convert myList as String into ArrayList

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-12
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2018-03-01
    相关资源
    最近更新 更多