简体   繁体   中英

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

I'm trying to post a form data using JQuery to a remote 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:

`

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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