javascript - Ajax 成功数据返回完整的 HTML 页面

标签 javascript jquery ajax json jsp

我正在使用带有日期选择器字段的 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
    }
  }); 
 }
});

我从数据库中获取 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 中的值。那么任何人都可以帮助我解决这个问题并让我知道我在哪里犯了错误......

最佳答案

我认为你应该在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
        }
      }); 
     }
    });

关于javascript - Ajax 成功数据返回完整的 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25341263/

相关文章:

javascript - 网页无法正确加载: '"JSON"is undefined' error

jquery - 在输入焦点上显示描述文本

javascript - 无法在ajax响应中获取数据id以解析为模态

jquery - 如何将响应值从 .post() 返回到我的函数中?

javascript - 使用 AJAX/jQuery 查找 THIS div 或 THAT div

javascript - 'animate()' 函数我的代码不起作用,但 'css()' 可以。怎么了?

javascript - 谷歌 : "EcmaScript 5 getters and setters for properties are discouraged" Why?

jQuery 不 append 元素

javascript - 向下滚动单个页面时是否可以更改网址

javascript - dom bfrtip 和 bsort false 不能一起使用数据表