简体   繁体   English

回调函数没有在jquery中执行

[英]callback function not executed in jquery

I was learning to send a get request in jquery. 我正在学习在jquery中发送get请求。 The following is the block of code which makes the get request. 以下是产生get请求的代码块。 I checked in my servlet that the get request was made successfully but the callback function was not invoked. 我在我的servlet中检查了get请求是否成功,但是没有调用回调函数。 I can't figure out the problem. 我无法弄清楚这个问题。

$.get('http://localhost:8080/PC_controlo/StatusServlet?id=1001',
function(data){
alert('hello');
});

And the browsers network tab shows this 浏览器网络选项卡显示了这一点

[01:21:26.759] GET http://localhost:8080/PC_controlo/StatusServlet?id=1001 [HTTP/1.1 200 OK 1ms]

+ +

This is what I have done in my servlet. 这就是我在servlet中所做的。

PrintWriter out = response.getWriter();
            out.write("Working");
            out.close();

and .fail(msg){ alert('Failed with message:'+JSON.stringify(msg)); 和.fail(msg){alert('失败并显示消息:'+ JSON.stringify(msg)); gave the following message 给出了以下信息

Failed with message: {"readyState":0,"responseText":"","status":0,"statusText":"error"}

Thanks in advance :). 提前致谢 :)。

I did a few tests with replacing the URL with Google.com, and I encountered the same problem. 我用Google.com替换了网址做了一些测试,我遇到了同样的问题。 I noticed if you use .done(), .always(), and .error(), you might have better luck figuring out exactly what is going on. 我注意到如果你使用.done(),. aways()和.error(),你可能会更好地确定究竟发生了什么。 Check out this JSFiddle: 看看这个JSFiddle:

http://jsfiddle.net/gQfsd/ http://jsfiddle.net/gQfsd/

$.get('http://www.google.com/')
.done(function(data) {
    alert('Success! Data loaded: ' + data);   
})
.fail(function(msg) {
    alert('Failed with message: ' + JSON.stringify(msg));
})
.always(function() {
    alert('Always Alerts');
});

The most likely scenario is that your request did not complete successfully, so your success handler function callback is not called, because there was no success. 最可能的情况是您的请求未成功完成,因此未调用成功处理程序函数回调,因为没有成功。

According to the jQuery docs $.get 's success handler function is: (emphasis mine) 根据jQuery docs $.get的成功处理函数是:(强调我的)

A callback function that is executed if the request succeeds. 如果请求成功则执行的回调函数

To get more control over the error handling of the AJAX request, It's usually recommended to use $.ajax instead, which you can read the documentation for here . 为了更好地控制AJAX请求的错误处理,通常建议使用$.ajax ,您可以在这里阅读文档 This will allow you to use an error callback that will trigger when the request wont complete for some reason. 这将允许您使用error回调,该error回调将在请求因某些原因未完成时触发。

$.get is just a shortcut method that delegates to $.ajax under the covers. $.get只是一个委托给$.ajax的快捷方法。


Why is your request failing? 为什么您的请求失败? No clue. 没有线索。 That's between your browser and the server you are talking to. 这是在您的浏览器和您正在与之交谈的服务器之间。 It could be a million things. 这可能是一百万件事。 Check the network tab of your browser development console and see what the request and response looks like. 检查浏览器开发控制台的网络选项卡,查看请求和响应的外观。

Probably a communication error with the call itself, or the call failed for some reason. 可能是与呼叫本身的通信错误,或者由于某种原因呼叫失败。 Check for failures too: 检查故障:

$.get('http://localhost:8080/PC_controlo/StatusServlet?id=1001')
 .success(function(result) {alert('success'); })
.error(function(jqXHR, textStatus, errorThrown) {alert(textStatus) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM