简体   繁体   中英

jquery ajax success functions not executing

I have a modal dialog form that submits data to a table. I want to give the user some feedback on the success/failure of the submission, but the success handlers aren't firing.

My code is

$.ajax({
  type: "POST",
  url: "includes/phpscripts.php?action=submitWiki",
  data: {"name": location, "location": locationName, "text": locationText, "user": userId},
  beforeSend: function(x){
    if (x && x.overrideMimeType){
      x.overrideMimeType("application/json;charset=UTF-8");
    }
  },
  async: false
}).done(function(data){
  if (data == "failure"){
    $("#submissionFailure").dialog("open");
  } else {
    $("#submissionSuccess").dialog("open");
    var count= 3;
    var id= setInterval(function(){
      count--;
      if (count == 0)
        $("#submissionSuccess").dialog("close");
    }, 1000);
  }
});

Another version had the if/else block within done defined as

success: function(data){
  if (data == "failure"){
    $("#submissionFailure").dialog("open");
  } else {
    $("#submissionSuccess").dialog("open");
    var count= 3;
    var id= setInterval(function(){
      count--;
      if (count == 0)
        $("#submissionSuccess").dialog("close");
    }, 1000);
  }
}

In Firebug, I can clearly see the return from the request is success due to an echo statement, but the success function is getting skipped over. I've set the breakpoint directly on the done line, and everything gets skipped over.

In addition, the posts are being recorded into the database.

your missing "dataType" which defaults to HTML/String or XML maybe I cant remember which one off the top of my head, but lets say your working with JSON, if the dataType: isn't set to JSON it won't interpret your backends feedback as valid and essentially will ignore it. so under type: "POST" put dataType:"json"

Like Chris ^ told you, add the datatype, then change this line

data: {"name": location, "location": locationName, "text": locationText, "user": userId}

To that

data: {name: "location", location: "locationName", text: "locationText", user: "userId"}

The quotes should be around the second parameters, unless they are variables. First parameters can be left like that.

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