[英]success callback function is not called when ajax call to servlet
I am trying to make dependent drop down list. 我试图使依赖下拉列表。 For that I have created a jsp page and in that calling servlet through ajax call .In the servlet I am using Json object for returning values for dropdown . 为此,我创建了一个jsp页面,并通过ajax调用来调用servlet。在servlet中,我使用Json对象返回dropdown的值。 Values are correctly coming in Json object But The request is getting completed with some error (as success method is not getting called instead error method is called ) . 值正确传入Json对象,但请求已完成,但出现了一些错误(因为未调用成功方法,而是调用了error方法)。
Here is my ajax code : 这是我的ajax代码:
$.ajax({ type: "GET",
url: "MyServlet?index="+listindex,
datatype: 'JSON',
error : function() {
alert("Error");
},
success : function(data) {
try{
var citiArray=JSON.parse(data);
if(citiArray != null){
for(var s=0;s<citiArray.length;s++){
var serial=citiArray[s];
//populating drop down list
$("#dname").append($("<option></option>").val(serial.authors1).html(serial.authors1));
}
}
}
catch(err){
alert(err);
}
}
});
My Servlet code :MyServlet.java
public class MyServlet extends HttpServlet {
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet()
{
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/orcl" ,"hr", "password");
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
response.setContentType("application/json");
PrintWriter out = response.getWriter();
String listindex = request.getParameter("index");
out.print(listindex);
String query2 = "select module from course where cname=?";
PreparedStatement st2 = con.prepareStatement(query2);
st2.setString(1,listindex);
ResultSet rs2=st2.executeQuery();
JSONArray uniList1 = new JSONArray();
while (rs2.next())
{
uniList1.add(rs2.getString("module"));
//System.out.println(rs2.getString("module"));
}
JSONObject obj = new JSONObject();
obj.put("key", uniList1 );
System.out.println(obj);
out.print(obj);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
web.xml:
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.dac.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
Thanks Rohit 谢谢罗希特
Send a response even if an exception is thrown 即使引发异常也发送响应
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
//send the response
out.print(e.getMessage());
}
You can have a detailed description of the error with: 您可以通过以下方式对错误进行详细说明:
error: function (theRequest, theStatus, theError)
{
alert(theRequest.responseText);
}
instead of 代替
error : function() {
alert("Error");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.