【问题标题】:Ajax Success data returns complete HTML pageAjax 成功数据返回完整的 HTML 页面
【发布时间】:2014-10-10 01:34:09
【问题描述】:

我正在使用带有日期选择器字段的iFrame 类 JSP 页面。从选择器中选择日期时,我使用 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
    }
  }); 
 }
});

我从 DB 中获取 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);

在标准输出中给我这样的结果:

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}]

但是 Ajax 成功中的警报提供了完整的 HTML 页面 :(。我需要这些数据并希望通过在 div 表中显示来填充同一 JSP 中的值。所以任何人都可以帮助我解决它并让我知道我在哪里做错了......

【问题讨论】:

  • 您是否尝试将数据类型设置为 json 以及服务器 (jsp) 上发生了什么?
  • 如果我使用 JSON 数据类型,我无法自己进行 AJAX 调用

标签: javascript jquery ajax json jsp


【解决方案1】:

设置 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);     
    }
});

然后在您的 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));
    }
  }); 
 }
});

你也忘了把$放在("#datepickerStart").val()的开头

更多参考结帐my app

【讨论】:

    【解决方案2】:

    我认为你应该在 ajax 调用中使用 get 而不是 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
            }
          }); 
         }
        });
    

    【讨论】:

    • 没有改进 :( 只返回相同的 HTML 内容
    • 您是否尝试从浏览器浏览相同的 URL,如果一切正确,它应该返回 JSON 对象,如果仍然返回完整页面,我建议您检查 JSP 页面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2016-11-12
    • 2018-11-24
    • 2019-03-12
    • 2015-07-15
    • 1970-01-01
    相关资源
    最近更新 更多