如何在AJAX中使用.done()取代成功:function()

时间:2022-03-25 20:17:12

I was told that success:function() is deprecated and I should use .done() instead. However, I don't know how to properly replace it in my ajax code. I'm sure it's a simple fix.

我被告知成功:不推荐使用function(),我应该使用.done()。但是,我不知道如何在我的ajax代码中正确替换它。我确定这是一个简单的修复。

Can you please show me what this code should look like? Thank you

你能告诉我这段代码应该是什么样的吗?谢谢

$.ajax({
                type: "POST",
                url: "confirm.php",
                dataType:"text",
                data: {name: $('#name').val(), submitter: 'yes' },
                success: function(data)
           {
              $("#mainForm").hide();
             $('#thankYou').show();
           }

       })

2 个解决方案

#1


4  

The success property is not deprecated, and there is no need to change the code. That is a misconception coming from the jqXHR.success method being deprecated.

不推荐使用success属性,并且无需更改代码。这是来自不推荐使用的jqXHR.success方法的误解。

Anyhow, this is how you would use the done method:

无论如何,这是你如何使用done方法:

$.ajax({
  type: "POST",
  url: "confirm.php",
  dataType:"text",
  data: {name: $('#name').val(), submitter: 'yes' }
}).done(function(data) {
  $("#mainForm").hide();
  $('#thankYou').show();
});

#2


2  

done function is chained in jQuery. It looks like:

完成函数链接在jQuery中。看起来像:

$.ajax(
 //ajax stuffs
 ...
 ...
).done(function(){
 // after ajax completion
});

#1


4  

The success property is not deprecated, and there is no need to change the code. That is a misconception coming from the jqXHR.success method being deprecated.

不推荐使用success属性,并且无需更改代码。这是来自不推荐使用的jqXHR.success方法的误解。

Anyhow, this is how you would use the done method:

无论如何,这是你如何使用done方法:

$.ajax({
  type: "POST",
  url: "confirm.php",
  dataType:"text",
  data: {name: $('#name').val(), submitter: 'yes' }
}).done(function(data) {
  $("#mainForm").hide();
  $('#thankYou').show();
});

#2


2  

done function is chained in jQuery. It looks like:

完成函数链接在jQuery中。看起来像:

$.ajax(
 //ajax stuffs
 ...
 ...
).done(function(){
 // after ajax completion
});