[英]Ajax Success data returns complete HTML page
I'm using iFrame
class JSP page with Date picker field on it. 我正在使用带日期选择器字段的
iFrame
类JSP页面。 On selecting a date from the picker, I'm sending the date to the Struts Action using jQuery AJAX call like below: 从选择器中选择日期后,我将使用jQuery AJAX调用将日期发送至Struts Action,如下所示:
$( "#datepickerStart" ).datepicker({
onSelect: function(dateText, instance) {//date select from picker to trigger
$.ajax({
type: "Post",// post method
url: 'checkAvailability.do?operation=getlist&datepickerStart='+ ("#datepickerStart").val(), // passing URL with date value to STRUTS Action
data: "date="+date,
//dataType: "application/json",
success: function(data) {
alert(data); //getting with the complete HTML page
}
});
}
});
And from DB I'm fetching the results in LIST and converting to JSON object like below: 从数据库中,我正在LIST中获取结果并转换为JSON对象,如下所示:
Gson gson = new Gson();// Using google GSON to convert to JSON
String json = new Gson().toJson(lRList);
response.setContentType("application/json");// setting content type
response.setCharacterEncoding("UTF-8"); //setting character encoder
response.getWriter().write(json);// writing to response the JSON object
System.out.println("JSON Object::"+json);
And in standard output gives me the result like this: 在标准输出中,结果如下:
JSON Object::[{"bookDate":"2014-07-11","fromTime":"2:00PM","totime":"3:30PM","userID":"XXX","isSuccess":false},
{"bookDate":"2014-07-11","fromTime":"10:30AM","totime":"11:00AM","userID":"XXX","isSuccess":false}]
But the alert in Ajax success gives the complete HTML page :(. I need this data and want to populate the values in the same JSP by showing in a div table. So can anyone help me on this to resolve it and let me know where I'm doing the mistake... 但是Ajax成功的警报提供了完整的HTML页面:(。我需要这些数据,并希望通过在div表中显示来填充同一JSP中的值。因此,任何人都可以帮助我解决该问题,并告诉我在哪里我做错了...
i think you should use get instead of post in the ajax call 我认为您应该使用get而不是ajax调用中的post
$( "#datepickerStart" ).datepicker({
onSelect: function(dateText, instance) {//date select from picker to trigger
$.ajax({
type: "get",
url: 'checkAvailability.do?operation=getlist&datepickerStart='+ ("#datepickerStart").val(), // passing URL with date value to STRUTS Action
data: "date="+date,
//dataType: "application/json",
success: function(data) {
alert(data); //getting with the complete HTML page
}
});
}
});
setup AJAX error to get the error like: 设置AJAX错误以获取如下错误:
$.ajaxSetup({
error: function(jqXHR, e) {
var msg = '';
if(jqXHR.status==0){
msg = 'You are offline!!\n Please Check Your Network.';
}else if(jqXHR.status==404){
msg = 'Requested URL not found.';
}else if(jqXHR.status==500){
msg = 'Internal Server Error.<br/>'+jqXHR.responseText;
}else if(e=='parsererror'){
msg = 'Error: Parsing JSON Request failed.';
}else if(e=='timeout'){
msg = 'Request Time out.';
}else {
msg = 'Unknow Error.<br/>'+x.responseText;
}
console.log('error: '+jqXHR.responseText);
console.log('Error msg: '+msg);
}
});
and then set datatype as json in your AJAX call as your getting response in Json format like: 然后在您的AJAX调用中将数据类型设置为json,作为您获得Json格式的响应,例如:
$("#datepickerStart").datepicker({
onSelect: function(dateText, instance) {
$.ajax({
type: "post",
url: 'checkAvailability.do?operation=getlist&datepickerStart='+$("#datepickerStart").val(),
data: "date="+date,
dataType: 'json',
success: function(data) {
alert(JSON.stringify(data));
}
});
}
});
also you were forgot to put $
starting of ("#datepickerStart").val()
你也忘了把
$
开始的("#datepickerStart").val()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.