java - servlet 未接收到 ajax

标签 java javascript jquery ajax servlets

我对 jQuery 和 $Ajax() 是全新的,但对 Java EE 非常熟悉,并且完全坚持这一点。

我有一个 js 函数和来自 firebug 中 javascript 控制台的信息,它向 servlet 发送请求 - 但消息永远不会到达 servlet。

当 get 或 post 到达 servlet 时,它总是会记录一些内容,但当 $Ajax()“调用”时它不会记录任何内容。

这是 fireBug 的一些输出:

about to call ajax()<br>
showCID.jsp (line 12)<br>

    GET http://localhost:8080/RHY_Tools/AjaxServlet?id=123<br>

jquery....min.js (line 6)<br>
done with call to ajax()<br>
showCID.jsp (line 41)  Error:<br>
showCID.jsp (line 30)  Status: error<br>
showCID.jsp (line 31)  readyState <br>

服务器日志中没有任何内容。

如果我粘贴

    http://localhost:8080/RHY_Tools/AjaxServlet?id=123 into the browser window the server log shows:

INFO: Id parameter is: 123<br>
INFO: done now.<br>

这是函数(上面的日志显示它被调用)

var myurl="AjaxServlet";
function checkCID(cid) {

console.log("about to call ajax()");

$.ajax({
    url:myurl ,
    data: {id: 123},
    type: "GET",

    success: function( resp ) {
        $( "" ).text( resp ).appendTo( "body" );
    },

    error: function( xhr, status, errorThrown ) {
        console.log( "Error: " + errorThrown );
        console.log( "Status: " + status );
        console.dir( xhr );
    },

    complete: function( xhr, status ) {
        alert( "The request is complete!" );
    }
});

console.log("done with call to ajax()");
}

最佳答案

一些建议:

  • url 不需要完整的主机(因为它是 ajax,默认情况下它会转到浏览器已连接的主机)。您尝试过 myurl = '/RHY_Tools/AjaxServlet?id=123' 吗?
  • 从 firebug 检查网络流量面板,看看从客户端的 Angular 来看是否收到响应(404、500、200 等),或者只是超时。这应该可以帮助您调试它实际到达的位置(如果有)
  • 最后,这可能只是上面的 post/html 中的拼写错误,但我看到 URL 末尾有“
    ”?

关于java - servlet 未接收到 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23232001/

相关文章:

java - 如何测试 Spring @Scheduled

javascript - AJAX - 检索多个响应值?

javascript - 为什么 jQuery 接受数组作为参数?

javascript - 即使使用 stopPropagation 也会触发父元素

javascript - MathJax 将宏定义为最终或最后一个

javascript - 当我添加数组时我破坏了这个JS

java - 在 Java 中实现 OAuth 提供程序

java - 在页面加载时从JSP文件调用servlet

java - 如何在 Hibernate 中的同一实体上使用不同的验证规则?

javascript - Ajax响应未调用成功:function() when using jQuery 1. 8.3