天天看點

如何在ajax的success裡面擷取到servlet傳回的值,并且代參跳轉頁面,請大神指教

<%@ page import="com.syxa.https.Search"%>
<%@ page import="com.syxa.https.dto.HcMessageDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="/DemoHttpClient/js/jquery-1.8.3.js"></script>

<script type="text/javascript">

function yj(data,carrier,flightNumber,depAirport,depTime,arrAirport,arrTime,stopCities,codeShare,cabin,aircraftCode){

	var oID=document.getElementById("cid").value;/* 拿到A_form中的id值 */
	var oHID=document.getElementById("id");		/* 拿到B_form中的隐藏域對象 */
	oHID.value=oID;								/* 為隐藏域對象的value指派 */
	
	
	var n=document.getElementById("tripTpye").value;
	var nt=document.getElementById("tripT");
	nt.value=n;
	
	var A=document.getElementById("adultNumber").value;
	var AN=document.getElementById("adultN");
	AN.value=A; 
	
	var C=document.getElementById("childNumber").value;
	var CN=document.getElementById("childN");
	CN.value=C;
	
	
	$.ajax({
        type : "POST",
        url : "demo.do?type=verify",
        data : {
        	"id":$("#id").val(),
        	"tripT":$("#tripT").val(),
        	"adultN":$("#adultN").val(),
        	"childN":$("#childN").val(),
        	"data":data,
        	"carrier":carrier,
        	"flightNumber":flightNumber,
        	"depAirport":depAirport,
        	"depTime":depTime,
        	"arrAirport":arrAirport,
        	"arrTime":arrTime,
        	"stopCities":stopCities,
        	"codeShare":codeShare,
        	"cabin":cabin,
        	"aircraftCode":aircraftCode 
        	},
        async : false,
        dataType : "json",
        success : function(data) {
			alert(data);
			/* window.open(DemoHttpClient/jsp/seand.jsp); */
			 if (data) {//根據傳回值進行跳轉
				 var dataValue = "/DemoHttpClient/jsp/seand.jsp?data"+data;
				 window.location.replace(dataValue);
            //window.location.href = "/DemoHttpClient/jsp/seand.jsp?data"+data;
        }
        },
        error:function(message){
        	alert("ajax參數錯誤!");
        }
    });
	
}
	</script>

</head>
<body>
	<div class="" align="center">
		<div class="row">
			<div class="">
				<div class="">
					<form action="demo.do?type=query" id="tf" method="post" onsubmit="SomeJavaScriptCode">
						<table class="" >
							<tr>
								<th>銷售管道:</th>
								<td>
									<select name="cid" id="cid">
										<!-- 	<option value="ctrip20180507">攜程-正航-ctrip</option>
											<option value="qunar20180511">去哪兒-正航-qunar</option>
											<option value="testcid001">testctrip-正航test-ctrip</option>
											<option value="ali20180525hf">阿裡航服-正航-ali</option>
											<option value="ali20180525jp">阿裡金牌-正航-ali</option>
											<option value="ali20180525js">阿裡極速-正航-ali</option>
											<option value="testcid002">testali-正航test-ali</option>
											<option value="test20180711tuniu">途牛-途牛test-tuniu</option> -->
											<option value="testcid003">testqunar-正航test-qunar</option>
									</select>
								</td>
								<th>航程類型:</th>
								<td>
									<select name="tripTpye" id="tripTpye">
											<option value="1">單程</option>
											<option value="roundTrip">往返</option>
									</select>
								</td>
								<th>成人</th>
								<td>
									<select name="adultNum" id="adultNumber">
											<option value="1">1</option>
											<option value="2">2</option>
											<option value="3">3</option>
											<option value="4">4</option>
											<option value="5">5</option>
											<option value="6">6</option>
									</select>
								</td>
								<th>兒童</th>
								<td>
									<select name="childNum" id="childNumber">
											<option value="0">0</option>
											<option value="1">1</option>
											<option value="2">2</option>
											<option value="3">3</option>
											<option value="4">4</option>
											<option value="5">5</option>
											<option value="6">6</option>
									</select>
								</td>
								<th>出發地:</th>
								<td><input name="fromCity" value="HKG" id="fromCity" type="test"></td>
								<th>目的地:</th>
								<td><input name="toCity" value="XMN" id="toCity" type="test"></td>
								<!-- <th>航司:</th>
								<td><input name="carrier" id="carrier" value="" type="text" ></td> -->
							</tr>
							<tr>
								<th>出發日期:</th>
								<td><input name="fromDate" value="20180901" /></td>
								<th>傳回日期:</th>
								<td><input name="retDate" value="" /></td>
								<!-- <th>航班号:</th>
								<td><input name="flightnumbers" value="" id="flightnumbers" type="text" class="form-control input-sm"></td> -->
								
							</tr>
						</table>
						<div align="center">
						<!-- <input type="button" class="btn btn-primary" onclick="search()" value="查詢"> -->
						<input type="submit" value="查詢"> 
						<input type="reset" value="取消">
						</div>
					</form>
					<br>
				</div>

			</div>
		</div>
	</div>
	<div class="" align="center" >
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox ">
					<div class="ibox-title"></div>
					<div class="ibox-content">
						<div class="zbss-table-wrapper table-responsive">
						<form action="demo.do?type=verify" method="post" id="tf" class="formdata">
							<table class="table table-striped table-hover table-condense"  cellspacing="0">
								<thead>
									<tr>
										<th>成人單價</th>
										<th>成人稅費</th>
										<th>成人稅費類型</th>
										<th>報價類型</th>
										<th>兒童單價</th>
										<th>兒童稅費</th>
										<th>兒童稅費類型</th>
										<th>data</th>
										<th>去程航段</th>
										<th>乘客國籍</th>
										<th>乘客國籍類型</th>
										<th>報價類型</th>
										<th>回程航段</th>
										<th>退改簽資訊</th>
										<th>适用乘客年齡</th>
										<th>報帳憑證類型</th>
										<th>出票速度</th>
										<th style="display: none;">銷售管道</th>
										<th style="display: none;">行程類型</th>
										<th style="display: none;">成人乘機總人 數</th>
										<th style="display: none;">兒童乘機總人 數</th>
										<th>操作</th>
									</tr>
								</thead>
								<tbody>
								<c:forEach var="list" items="${requestScope.tlist}">
									<tr>
										<td>${list.adultPrice }</td>
										<td>${list.adultTax }</td>
										<td>${list.adultTaxType }</td>
										<td>${list.applyType }</td>
										<td>${list.childPrice }</td>
										<td>${list.childTax }</td>
										<td>${list.childTaxType }</td>
										<td class="data">${list.data }</td>
										<%-- <td><input type="text" value="${list.data }" name="data" id="data"></td> --%>
										<%-- <td><input type="text" value="${list.fromSegments }" name="fromSegments" id="fromSegments"></td> --%>
										<!-- <td><textarea name="fromSegments" value="{\"carrier\": \"MF\",\"stopAirports\": \"\",\"canbinClass\": 1,\"stopCities\": \"\",\"codeShare\": false}" id="fromSegments"></textarea></td> -->
										<%-- <td>${list.fromSegments }</td> --%>
										<td>
											航司IATA二字碼:${list.hc.carrier }
											航班号:${list.hc.flightNumber }
											出發機場IATA三字碼 :${list.hc.depAirport }
											起飛日期時間:${list.hc.depTime }1
											到達機場IATA三字碼 :${list.hc.arrAirport }
											到達日期時間:${list.hc.arrTime }
											經停地:${list.hc.stopCities }
											代碼共享辨別:${list.hc.codeShare }
											艙位 :${list.hc.cabin }
											機型 :${list.hc.aircraftCode }
											艙位等級:${list.hc.canbinClass }
											經停時間:${list.hc.stopAirports }
										</td>
										<td>${list.nationality }</td>
										<td>${list.nationalityType }</td>
										<td>${list.priceType }</td>
										<td>${list.rule }</td>
										<%-- <td><input type="" value="${list.retSegments }" name="retSegments" id="retSegments"></td> --%>
										<%-- <td><textarea name="retSegments" value="${list.retSegments}" id="retSegments"></textarea></td> --%>
										<td class="retSegments">${list.retSegments}</td>
										<td>${list.suitAge }</td>
										<td>${list.ticketInvoiceType }</td>
										<td>${list.ticketTimeLimit }</td>
										<td style="display: none;"><input type="hidden" value="" name="cid" id="id" title="testqunar-正航test-qunar" ></td>
										<td style="display: none;"><input class="tripTpye" type="hidden" value="" name="tripTpye" id="tripT"></td>
										<td style="display: none;"><input class="adultNum" type="hidden" value="" name="adultNum" id="adultN"></td>
										<td style="display: none;"><input class="childNum" type="hidden" value="" name="childNum" id="childN"></td>
										<!-- <td><input type="hidden" value="" name="data" id="data2"></td>
										<td><input type="hidden" value="" name="fromSegments" id="fromSegments2"></td>
										<td><input type="hidden" value="" name="retSegments" id="retSegments2"></td> -->
										<td><input id="input" type="button" value="驗價" onclick="yj('${list.data}','${list.hc.carrier }','${list.hc.flightNumber }','${list.hc.depTime }','${list.hc.depAirport }','${list.hc.arrAirport }','${list.hc.arrTime }','${list.hc.stopCities }','${list.hc.codeShare }','${list.hc.cabin }','${list.hc.aircraftCode}')"></td>
									</tr>
								</c:forEach>
								</tbody>
							</table>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
</html>
           

上面是index,jsp

 servlet

public void verify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		String url = "http://127.0.0.1:8082/order/priceVerify";
				
		String data = request.getParameter("data");
		String carrier = request.getParameter("carrier");
		String flightNumber = request.getParameter("flightNumber");
		String depAirport = request.getParameter("depAirport");
		String depTime = request.getParameter("depTime");
		String arrAirport = request.getParameter("arrAirport");
		String arrTime = request.getParameter("arrTime");
		String stopCities = request.getParameter("stopCities");
		String codeShare = request.getParameter("codeShare");
		String cabin = request.getParameter("cabin");
		String aircraftCode = request.getParameter("aircraftCode");
	
		HcMessageDTO hDto = new HcMessageDTO();
		hDto.setCarrier(carrier);
		hDto.setFlightNumber(flightNumber);
		hDto.setDepAirport(depAirport);
		hDto.setDepTime(depTime);
		hDto.setArrAirport(arrAirport);
		hDto.setArrTime(arrTime);
		hDto.setStopCities(stopCities);
		hDto.setCodeShare(codeShare);
		hDto.setCabin(cabin);
		hDto.setAircraftCode(aircraftCode);
		
		JSONObject jo=new JSONObject();
		jo.put("carrier", hDto.getCarrier());
		jo.put("flightNumber", hDto.getFlightNumber());
		jo.put("depAirport", hDto.getDepAirport());
		jo.put("depTime", hDto.getDepTime());
		jo.put("arrAirport", hDto.getArrAirport());
		jo.put("arrTime", hDto.getArrTime());
		jo.put("stopCities", hDto.getStopCities());
		jo.put("codeShare", hDto.getCodeShare());
		jo.put("cabin", hDto.getCabin());
		jo.put("aircraftCode", hDto.getAircraftCode());
		//将JSONObject轉換成json
		String json = jo.toString();
		//構造person JSONObject數組
		List<JSONObject> fromSegments=new ArrayList<JSONObject>(); 
		fromSegments.add(jo);
		JSONObject routing =new JSONObject();
		routing.put("data", data);
		routing.put("fromSegments", fromSegments);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("cid", id);
		jsonObject.put("tripType",tripT);
		jsonObject.put("adultNum", adultN);
		jsonObject.put("childNum", childN);
		jsonObject.put("routing", routing);
		String json1=jsonObject.toString();
		System.out.println(json1);
		
		String httpResult = new HttpRequest().sendPost(url, json1.toString());
		System.out.println(httpResult);
		//先擷取jsonobject對象
		JSONObject jObject = JSONObject.parseObject(httpResult);
		
		//從jsonobject對象中取出routing
		String routings = jObject.getString("routing");
		//jsonobject轉java對象
		Routing routing2 = JSON.parseObject(routings, Routing.class);
		System.out.println("routing2+++++"+routing2);
		Routing r = new Routing();
		r.setAdultPrice(routing2.getAdultPrice());
		System.out.println("getAdultPrice"+routing2.getAdultPrice());
		r.setAdultTax(routing2.getAdultTax());
		r.setAdultTaxType(routing2.getAdultTaxType());
		r.setApplyType(routing2.getApplyType());
		r.setChildPrice(routing2.getChildPrice());
		r.setChildTax(routing2.getChildTax());
		r.setChildTaxType(routing2.getChildTaxType());
		r.setData(routing2.getData());
		String fromSegment = routing2.getFromSegments();
		//string轉jsonarray
		JSONArray jsonArray=JSONArray.parseArray(fromSegment);
		System.out.println("jsonArray---"+jsonArray);
		for (int i = 0; i < jsonArray.size(); i++) {
			//jsonArray轉jsonobject
			JSONObject jsonObject2 = jsonArray.getJSONObject(i);
			System.out.println("jsonObject2---"+jsonObject2);
			//jsonobject轉java對象
			HcMessageDTO dto = JSONObject.toJavaObject(jsonObject2, HcMessageDTO.class);
			HcMessageDTO hc=new HcMessageDTO();
			hc.setAircraftCode(dto.getAircraftCode());
			hc.setArrAirport(dto.getArrAirport());
			hc.setArrTime(dto.getArrTime());
			hc.setCabin(dto.getCabin());
			hc.setCanbinClass(dto.getCanbinClass());
			hc.setCarrier(dto.getCarrier());
			hc.setCodeShare(dto.getCodeShare());
			hc.setDepAirport(dto.getDepAirport());
			hc.setDepTime(dto.getDepTime());
			hc.setFlightNumber(dto.getFlightNumber());
			hc.setStopAirports(dto.getStopAirports());
			hc.setStopCities(dto.getStopCities());
			List<HcMessageDTO> list = new ArrayList<HcMessageDTO>();
			list.add(hc);
			r.setFromSegment(list);
			System.out.println("list+++"+list.toArray());
		}
		
		r.setNationality(routing2.getNationality());
		r.setNationalityType(routing2.getNationalityType());
		r.setPriceType(routing2.getPriceType());
		r.setRetSegments(routing2.getPriceType());
		r.setRetSegments(routing2.getRetSegments());
		r.setSuitAge(routing2.getSuitAge());
		r.setTicketInvoiceType(routing2.getTicketInvoiceType());
		r.setTicketTimeLimit(routing2.getTicketTimeLimit());
		System.out.println("routingLlist---"+r.getFromSegments());
		System.out.println("routingLlist---"+r);
		//從jsonobject對象中取出rule
		String rule = jObject.getString("rule");
		//jsonobject轉java對象
		Rules rules = JSONObject.parseObject(rule, Rules.class);
		Rules ru = new Rules();
		ru.setBaggage(rules.getBaggage());
		ru.setEndorse(rules.getEndorse());
		ru.setEndorsement(rules.getEndorsement());
		ru.setHasBaggage(rules.getHasBaggage());
		ru.setHasEndorse(rules.getHasEndorse());
		ru.setHasNoShow(rules.getHasNoShow());
		ru.setHasRefund(rules.getHasRefund());
		ru.setNoShowLimitTime(rules.getNoShowLimitTime());
		ru.setOther(rules.getOther());
		ru.setPartEndorse(rules.getPartEndorse());
		ru.setPartEndorsePrice(rules.getPartEndorsePrice());
		ru.setPartRefund(rules.getPartRefund());
		ru.setPartRefundPrice(rules.getPartRefundPrice());
		ru.setPenalty(rules.getPenalty());
		ru.setRefund(rules.getRefund());
		ru.setSpecialNoShow(rules.getSpecialNoShow());
		System.out.println("rules---"+ru);
		
		Verify verify = JSONObject.toJavaObject(jObject, Verify.class);
		Verify verifys = new Verify();
		verifys.setMaxSeats(verify.getMaxSeats());
		verifys.setRouting(r);
		verifys.setRules(ru);
		verifys.setSessionId(verify.getSessionId());
		
		System.out.println(verify.getSessionId());
		System.out.println();
		JSONObject js = (JSONObject) JSONObject.toJSON(verifys);
		String verifyJson = js.toJSONString();
		System.out.println("verifyJson+++++"+verifyJson);
//		request.setAttribute("verify", verifys);
		PrintWriter out = response.getWriter();
		out.println(verifyJson);   
           

如何在seand.jsp 頁面擷取到資料,index.jsp中點選驗價按鈕然後跳到此方法查詢,查出以後傳回json(傳回的資料是從一個接口裡面擷取到的,是以與傳進去的資料不一樣),success裡面如何擷取servlet傳回的結果?并且把資料顯示到seand.jsp頁面上去

下面是seand.jsp

<body>
<%
	Verify verify = (Verify)request.getAttribute("dataValue");
	//String verify = request.getParameter("dataValue");
%>
	<div class="zbss-table-wrapper table-responsive">
		<form action="demo.do?type=verify" method="post" id="tf" class="formdata">
			<table class="table table-striped table-hover table-condense"  cellspacing="0">
				<thead>
					<tr>
						<th>可預訂的座位數</th>
						<th>成人單價</th>
						<th>成人稅費</th>
						<th>成人稅費類型</th>
						<th>報價類型</th>
						<th>兒童單價</th>
						<th>兒童稅費</th>
						<th>兒童稅費類型</th>
						<th>data</th>
						<th>去程航段</th>
						<th>乘客國籍</th>
						<th>乘客國籍類型</th>
						<th>報價類型</th>
						<th>回程航段</th>
						<th>适用乘客年齡</th>
						<th>報帳憑證類型</th>
						<th>出票速度</th>
						<th>退改簽資訊</th>
						<th>會話辨別</th>
						<th style="display: none;">銷售管道</th>
						<th style="display: none;">行程類型</th>
						<th style="display: none;">訂單來源</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
						<tr>
							<%-- <td><%=verify.getMaxSeats()%> </td> --%>
							<td>${verify.getMaxSeats() }</td>
							<td>${verify.getRouting().getAdultPrice() }</td>
							<td>${verify.getRouting().getAdultTax() }</td>
							<td>${verify.getRouting().getAdultTaxType() }</td>
							<td>${verify.getRouting().getApplyType() }</td>
							<td>${verify.getRouting().getChildPrice() }</td>
							<td>${verify.getRouting().getChildTax() }</td>
							<td>${verify.getRouting().getChildTaxType() }</td>
							<td class="data">${verify.getRouting().getData() }</td>
							<td>
								航司IATA二字碼:${verify.getRouting().getFromSegments().getCarrier() }
								航班号:${verify.getRouting().getFromSegments().getFlightNumber() }
								出發機場IATA三字碼 :${verify.getRouting().getFromSegments().getDepAirport() }
								起飛日期時間:${verify.getRouting().getFromSegments().getDepTime() }
								到達機場IATA三字碼 :${verify.getRouting().getFromSegments().getArrAirport() }
								到達日期時間:${verify.getRouting().getFromSegments().getArrTime() }
								經停地:${verify.getRouting().getFromSegments().getStopCities() }
								代碼共享辨別:${verify.getRouting().getFromSegments().getCodeShare() }
								艙位 :${verify.getRouting().getFromSegments().getCabin() }
								機型 :${verify.getRouting().getFromSegments().getAircraftCode() }
								艙位等級:${verify.getRouting().getFromSegments().getCanbinClass() }
								經停時間:${verify.getRouting().getFromSegments().getStopAirports() }
							</td>
							<td>${verify.getRouting().getNationality() }</td>
							<td>${verify.getRouting().getNationalityType() }</td>
							<td>${verify.getRouting().getPriceType() }</td>
							<td>${verify.getRouting().getRetSegments() }</td>
							<td>${verify.getRouting().getSuitAge() }</td>
							<td>${verify.getRouting().getTicketInvoiceType() }</td>
							<td>${verify.getRouting().getTicketTimeLimit() }</td>
							<td>
							單程直飛時: ${verify.getRules().getBaggage() }
							改簽标準: ${verify.getRules().getEndorse() }
							簽轉規則: ${verify.getRules().getEndorsement() }
							是否提供免費行李額:${verify.getRules().getHasBaggage() }
							是否允許改期:${verify.getRules().getHasEndorse() }
							是否有noshow規則:${verify.getRules().getHasNoShow() }
							是否允許退票:${verify.getRules().getHasRefund() }
							Noshow時限:${verify.getRules().getNoShowLimitTime() }
							其他事項:${verify.getRules().getOther() }
							部分改期規則:${verify.getRules().getPartEndorse() }
							部分改期費用:${verify.getRules().getPartEndorsePrice() }
							部分退票規則:${verify.getRules().getPartRefund() }
							部分退票費用:${verify.getRules().getPartRefundPrice() }
							Noshow罰金:${verify.getRules().getPenalty() }
							退款規則:${verify.getRules().getRefund() }
							Noshow特殊規則:${verify.getRules().getSpecialNoShow() }
							</td>
							<td>${verify.getSessionId() }</td>
							<td><input type="button" id="sd" value="生單" onclick="sd()"/></td>
						</tr>
				</tbody>
			</table>
		</form>
	</div>
</body>