简体   繁体   English

jQuery ajax()完成/失败回调未返回状态200

[英]JQuery ajax() done / fail callbacks not returning upon status 200

I'm trying to post a form data using JQuery to a remote servlet. 我正在尝试使用JQuery将表单数据发布到远程servlet。 I can see that the server receives the data and also returns status code 200 and a response string of "{result: 'success'}" But the ajax call doesn't return with the done or fail functions (if I add an always function than I can see that it is being called) Here's a code snippet of the client side: 我可以看到服务器接收到数据并且还返回状态代码200和响应字符串“ {result:'success'}”,但是ajax调用不会返回完成函数或失败函数(如果我添加了always函数可以看到它被调用了)这是客户端的代码段:

` `

var dataParams = 'email='+email+'&password='+password;
var url = 'http://127.0.0.1:8888/signup';

var jxhr = $.ajax({
  type : "POST",
  url : url,
  data : dataParams,// serializes the form's elements.
  dataType: "json",
  done: function() {
     console.log("done!");
     hideSignUp();
     showThankYou(); },
  fail: function() {
     console.log("fail!");
      }
  });

` `

Seems like I'm missing out on something, but can't seem to find what. 似乎我错过了某些东西,但似乎找不到什么。 Note that I'm using JQuery 1.8.3 so success is deprecated. 请注意,我正在使用JQuery 1.8.3,因此不赞成使用成功。 Any thoughts? 有什么想法吗?

Try: 尝试:

var url = "http://127.0.0.1:8888/signup";
var jxhr = $.ajax({
  type : "POST",
  url : url,
  data : dataParams,// serializes the form's elements.
  dataType: "json"
  }).done(function() {
     console.log("done!");
     hideSignUp();
     showThankYou(); 
  }).fail(function(jqXHR, textStatus) {
     console.log(textStatus);
});

Try chaining your callbacks, rather than setting them as object fields: 尝试链接回调,而不是将它们设置为对象字段:

 $.ajax({
  type : "POST",
  url : url,
  data : dataParams,// serializes the form's elements.
  dataType: "json"
  }).done(function (xhrResponse) {
    console.log("done!");
    hideSignUp();
    showThankYou();
  }).fail(function (xhrResponse, textStatus) {
    console.log(textStatus);
  }).always( function () {
    console.log("I'm done with this.");
  });

By chaining your callbacks, you guarantee execution of at least one (complete). 通过链接回调,可以保证至少执行一个(完成)。

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

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