Java json ajax问题
我很抱歉问这个问题,但我已经为此工作了好几个小时,我无法独自解决这个问题 我必须为项目的一部分使用json,我能够让它工作,但现在它没有将它返回到正确的jsp,而是只显示json jsp。我很确定这就是我接收json的方式 以下是正在发生的情况的屏幕截图: 这是我需要使用ajax的jsp,我想使用ajax填充第二个下拉列表: 这就是正在发生的情况,(这是正确的数据): 这是代码(抱歉太长): -我正在使用的jsp是ajaxJava json ajax问题,java,javascript,ajax,json,jsp,Java,Javascript,Ajax,Json,Jsp,我很抱歉问这个问题,但我已经为此工作了好几个小时,我无法独自解决这个问题 我必须为项目的一部分使用json,我能够让它工作,但现在它没有将它返回到正确的jsp,而是只显示json jsp。我很确定这就是我接收json的方式 以下是正在发生的情况的屏幕截图: 这是我需要使用ajax的jsp,我想使用ajax填充第二个下拉列表: 这就是正在发生的情况,(这是正确的数据): 这是代码(抱歉太长): -我正在使用的jsp是ajax <script type="text/javascri
<script type="text/javascript">
/**
* Utility function to create the Ajax request object in a cross-browser way.
* The cool thing about this function is you can send the parameters in a two-dimensional
* array. It also lets you send the name of the function to call when the response
* comes back.
*
* This is a generalized function you can copy directly into your code. *
*/
function doAjax(responseFunc, url, parameters) {
// create the AJAX object
var xmlHttp = undefined;
if (window.ActiveXObject){
try {
xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
} catch (othermicrosoft){
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {}
}
}
if (xmlHttp == undefined && window.XMLHttpRequest) {
// If IE7+, Mozilla, Safari, etc: Use native object
xmlHttp = new XMLHttpRequest();
}
if (xmlHttp != undefined) {
// open the connections
xmlHttp.open("POST", url, true);
// callback handler
xmlHttp.onreadystatechange = function() {
// test if the response is finished coming down
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
// create a JS object out of the response text
var obj = eval("(" + xmlHttp.responseText + ")");
// call the response function
responseFunc(obj);
}
}
// create the parameter string
// iterate the parameters array
var parameterString = "";
for (var i = 0; i < parameters.length; i++) {
parameterString += (i > 0 ? "&" : "") + parameters[i][0] + "=" + encodeURI(parameters[i][1]);
}
// set the necessary request headers
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", parameterString.length);
xmlHttp.setRequestHeader("Connection", "close");
// send the parameters
xmlHttp.send(parameterString);
}
}//doAjax
/**
* Submits the guess to the server. This is the event code, very much
* like an actionPerformed in Java.
*/
function getSeats() {
// this is how you get a reference to any part of the page
var packInput = document.getElementById("pack");
var pack = packInput.value;
// while (packInput.childNodes.length > 0) { // clear it out
// aSeats.removeChild(aSeats.childNodes[0]);
// }
// an example of how to do an alert (use these for debugging)
// I've just got this here so that we know the event was triggered
//alert("You guessed " + seat);
// send to the server (this is relative to our current page)
// THIS IS THE EXAMPLE OF HOW TO CALL AJAX
doAjax(receiveAnswer, "ttp.actions.Sale3PackAction.action",
[["pack", pack]]);
// change the history div color, just 'cause we can
// var randhex = (Math.round(0xFFFFFF * Math.random()).toString(16) + "000000").replace(/([a-f0-9]{6}).+/, "#$1").toUpperCase();
// document.getElementById("history").style.background = randhex;
}
/**
* Receives the response from the server. Our doAjax() function above
* turns the response text into a Javascript object, which it sends as the
* single parameter to this method.
*/
function receiveAnswer(response) {
// show the response pack. For this one, I'll use the innerHTML property,
// which simply replaces all HTML under a tag. This is the lazy way to do
// it, and I personally don't use it. But it's really popular and you are
// welcome to use it. Just know your shame if you do it...
var messageDiv = document.getElementById("aSeats");
messageDiv.innerHTML = response.aSeats;
// replace our history by modifying the dom -- this is the right way
// for simplicity, I'm just erasing the list and then repopulating it
var aSeats = document.getElementById("aSeats");
while (aSeats.childNodes.length > 0) { // clear it out
aSeats.removeChild(aSeats.childNodes[0]);
}
for (var i = 0; i < response.history.length; i++) { // add the items back in
var option = aSeats.appendChild(document.createElement("option"));
option.appendChild(document.createTextNode(response.history[i]));
}
// reset the input box
//document.getElementById("pack").value = "";
}
</script>
<% Venue v = (Venue)session.getAttribute("currentVenue"); %>
<% List<Conceptual_Package> cpList = Conceptual_PackageDAO.getInstance().getByVenue(v.getId()); %>
What Packages do you want to see?
<form method="post" action="ttp.actions.Sale3PackAction.action">
<select name="packid" id="pack">
<% for (Conceptual_Package cp: cpList) { %>
<option value="<%=cp.getId()%>"><%=cp.getName1()%></option>
<% } %>
</select>
<input type="submit" value=" next " onclick="getSeats();"/>
</form>
<!--new-->
Available Seats:
<select name="eventSeatid" id="aSeats">
<option value="aSeats"></option>
</select>
<input type="button" value=" Add "/>
Selected Seats:
<form method="post" action="ttp.actions.sale4Action.action">
<select name="eventSeat2id" size="10" id="seat2">
<option value="seat2"></option>
</select>
</form>
<jsp:include page="/footer.jsp"/>
/**
*以跨浏览器方式创建Ajax请求对象的实用函数。
*这个函数最酷的一点是,你可以用二维格式发送参数
*数组。它还允许您在响应时发送要调用的函数的名称
*回来了。
*
*这是一个可以直接复制到代码中的通用函数*
*/
函数doAjax(responseFunc、url、参数){
//创建AJAX对象
var xmlHttp=未定义;
if(window.ActiveXObject){
试一试{
xmlHttp=新的ActiveXObject(“MSXML2.xmlHttp”);
}捕获(其他Microsoft){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}捕获(失败){}
}
}
if(xmlHttp==undefined&&window.XMLHttpRequest){
//如果IE7+、Mozilla、Safari等:使用本机对象
xmlHttp=新的XMLHttpRequest();
}
if(xmlHttp!=未定义){
//打开连接
open(“POST”,url,true);
//回调处理程序
xmlHttp.onreadystatechange=函数(){
//测试响应是否完成
if(xmlHttp.readyState==4&&xmlHttp.status==200){
//从响应文本中创建JS对象
var obj=eval(“+xmlHttp.responseText+”);
//调用响应函数
响应函数(obj);
}
}
//创建参数字符串
//迭代参数数组
var参数字符串=”;
对于(var i=0;i0?&):“)+参数[i][0]+“=”+编码URI(参数[i][1]);
}
//设置必要的请求头
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
setRequestHeader(“Content-length”,parameterString.length);
setRequestHeader(“连接”,“关闭”);
//发送参数
send(参数字符串);
}
}//多贾克斯
/**
*将猜测提交给服务器。这是非常重要的事件代码
*就像在Java中执行的操作一样。
*/
函数getSeats(){
//这是获取对页面任何部分的引用的方式
var packInput=document.getElementById(“pack”);
var pack=packInput.value;
//而(packInput.childNodes.length>0){//将其清除
//aSeats.removeChild(aSeats.childNodes[0]);
// }
//如何执行警报的示例(使用这些进行调试)
//我刚拿到这个,这样我们就知道事件是被触发的
//警惕(“你猜对了”+座位);
//发送到服务器(这是相对于当前页面的)
//这是如何调用AJAX的示例
doAjax(receiveAnswer,“ttp.actions.Sale3PackAction.action”,
[pack,pack]];
//更改历史记录div的颜色,因为我们可以
//var randhex=(Math.round(0xFFFFFF*Math.random()).toString(16)+“000000”).replace(/([a-f0-9]{6})。+/,“#$1”).toUpperCase();
//document.getElementById(“历史”).style.background=randhex;
}
/**
*从服务器接收响应。上面的doAjax()函数
*将响应文本转换为Javascript对象,并作为
*此方法只有一个参数。
*/
功能接收应答(应答){
//显示响应包。对于这个,我将使用innerHTML属性,
//它只是替换标签下的所有HTML。这是一种懒惰的方法
//我个人不使用它,但它很受欢迎,而你也很受欢迎
//欢迎使用它。如果你这样做了,就要知道你的羞耻。。。
var messageDiv=document.getElementById(“aSeats”);
messageDiv.innerHTML=response.aSeats;
//通过修改dom替换我们的历史记录——这是正确的方法
//为了简单起见,我只是删除列表,然后重新填充它
var aSeats=document.getElementById(“aSeats”);
而(aSeats.childNodes.length>0){//将其清除
aSeats.removeChild(aSeats.childNodes[0]);
}
对于(var i=0;i
-json jsp
<%@page contentType="text/plain" pageEncoding="UTF-8"%>
<jsp:directive.page import="java.util.*"/>
{
"history": [
<% for (String newSeats: (List<String>)session.getAttribute("newSeats")) { %>
"<%=newSeats%>",
<% } %>
]
}
{
“历史”:[
"",
]
}
-动作类
public class Sale3PackAction implements Action{
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
String packid = request.getParameter("packid");
System.out.println("packid is: " + packid);
Conceptual_Package cp = Conceptual_PackageDAO.getInstance().read(packid);
request.setAttribute("cp", cp);
List<Physical_Package> ppList = Physical_PackageDAO.getInstance().getByConceptual_Package(cp.getId());
request.setAttribute("currentPack", ppList);
session.setAttribute("aSeats", null);
//return "sale3Pack_ajax.jsp";
//new
//HttpSession session = request.getSession();
// ensure we have a history
for (Physical_Package pPack: ppList){
try {
if (session.getAttribute("aSeats") == null) {
LinkedList aSeatsList = new LinkedList<String>();
session.setAttribute("aSeats", aSeatsList);
aSeatsList.add("Sec: " + pPack.getVenueSeat().getRowInVenue().getSectionInVenue().getSectionNumber() + " Row: " + pPack.getVenueSeat().getRowInVenue().getRowNumber() + " Seat: " + pPack.getVenueSeat().getSeatNumber());
session.setAttribute("newSeats", aSeatsList);
} else {
LinkedList aSeatsList = (LinkedList) session.getAttribute("aSeats");
aSeatsList.add("Sec: " + pPack.getVenueSeat().getRowInVenue().getSectionInVenue().getSectionNumber() + " Row: " + pPack.getVenueSeat().getRowInVenue().getRowNumber() + " Seat: " + pPack.getVenueSeat().getSeatNumber());
session.setAttribute("newSeats", aSeatsList);
}
} catch (DataException ex) {
Logger.getLogger(Sale3PackAction.class.getName()).log(Level.SEVERE, null, ex);
}
}
// next jsp page to go to
return "AjaxPack_json.jsp";
}
}
public类Sale3PackAction实现操作{
公共字符串进程(HttpServletRequest请求、HttpServletResponse响应)引发异常{
HttpSession session=request.getSession();
字符串packid=request.getParameter(“packid”);
System.out.println(“packid是:+packid”);
conceptional_Package cp=conceptional_PackageDAO.getInstance().read(packid);
setAttribute(“cp”,cp);
List ppList=Physical_PackageDAO.getInstance().getByConceptual_包(cp.getId());
setAttribute(“currentPack”,ppList);
session.setAttribute(“aSeats”,null);
//返回“sale3Pack_ajax.jsp”;
//新的
//HttpSession session=request.getSession();
//确保我们有历史
用于(物理包装pPack:ppList){
试一试{
if(session.getAttribute(“aSeats”)==null){
LinkedList aSeatsList=新建LinkedList();