我有两个jsp页面。 newEmployee.jsp
和editData.jsp
都有一个与myjScript.js相关联的srcipt。一旦用户在newEmployee.jsp
中提交州和国家/地区信息,就会转到editData.jsp
。
现在,在editData.jsp
的脚本(即同一页面)部分,我有一个ajax调用,它应该触发到SelectEditData.java
,但它说没有找到。 (这是servlet)。
@WebServlet("/SelectEditData")
public class SelectEditData extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("inside edit data servlet");
JsonArray data_json = new JsonArray();
try {
Connection con = ConnectionClass.getConnecton();
Statement st = con.createStatement();
String query = "SELECT * FROM selectboxschema.new_table";
System.out.println("details query " + query);
ResultSet rs = st.executeQuery(query);
JsonObject json_response = new JsonObject();
while (rs.next()) {
System.out.println("result set: " + rs.getString(1));
System.out.println("result set: " + rs.getString(2));
JsonObject json = new JsonObject();
json.addProperty("firstData", rs.getString(1));
json.addProperty("secondData", rs.getString(2));
data_json.add(json);
}
json_response.add("aData", data_json);
System.out.println("json response: " + json_response.toString());
response.setContentType("application/Json");
response.getWriter().write(json_response.toString());
} catch (Exception e) {
System.out.println("exception caught in edit data servlet" + e.getStackTrace());
}
}
}
//this is jScript.js
function populateStates(countryElementId, stateElementId) {
var selectedCountryIndex = document.getElementById(countryElementId).selectedIndex;
var stateElement = document.getElementById(stateElementId);
stateElement.length = 0; // Fixed by Julian Woods
stateElement.options[0] = new Option('Select State', '');
stateElement.selectedIndex = 0;
var state_arr = s_a[selectedCountryIndex].split("|");
for (var i = 0; i < state_arr.length; i++) {
stateElement.options[stateElement.length] = new Option(state_arr[i],
state_arr[i]);
}
}
function populateCountries(countryElementId, stateElementId) {
// given the id of the <select> tag as function argument, it inserts
// <option> tags
var countryElement = document.getElementById(countryElementId);
countryElement.length = 0;
countryElement.options[0] = new Option('Select Country', '-1');
countryElement.selectedIndex = 0;
for (var i = 0; i < country_arr.length; i++) {
countryElement.options[countryElement.length] = new Option(
country_arr[i], country_arr[i]);
}
// Assigned all countries. Event listener for the states.
if (stateElementId) {
countryElement.onchange = function() {
populateStates(countryElementId, stateElementId);
};
}
}
&#13;
//this is the editData.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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="${pageContext.request.contextPath}/js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jScript.js"></script>
<script type="text/javascript">
alert("ready of edit");
$.ajax({
type : "GET",
url : "SelectBoxEdit/SelectEditData",
dataType : "json",
success : function(data) {
console.log(data);
$.each(data.aData, function(i, obj) {
alert("data: " + obj.firstTable);
var div_data = "<option selected="
+ "selected" + ">"
+ obj.firstData + "</option>";
alert(div_data);
console.log(div_data);
$(div_data).appendTo('#country');
});
}
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div>
<hr/>
<br/>Select Country (with states):
<select id="country" name="country"></select>
<br />State:
<select name="state" id="state"></select>
<br/>
<script language="javascript">
populateCountries("country", "state");
</script>
<br/>
<br/>
<br />
<br />
<input type="button" id="submitBtn">
</div>
</body>
</html>
&#13;
答案 0 :(得分:0)
确保您的请求网址正确无误。
/yourServletPath
,这会生成http://localhost:8080/yourServletPath之类的请求网址。没有上下文路径。<input type="hidden" id="request_url" value="<%=request.getContextPath() %>/yourServletPath"/>
,然后在执行ajax请求时,您可以使用javascript代码获取它,例如:$('#request_url').val()
。