jQuery 使用 beforeSend 并用 $.post 完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2257975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:08:03  来源:igfitidea点击:

using beforeSend and complete with $.post?

jqueryajax

提问by ajsie

could one use the beforeSend() and complete() handlers with $.post or do you have to use $.ajax for it?

可以将 beforeSend() 和 complete() 处理程序与 $.post 一起使用,还是必须使用 $.ajax ?

回答by Nick Craver

You have 2 options, use $.ajax()or $.ajaxSetup().

您有 2 个选项,使用$.ajax()$.ajaxSetup()

Using $.ajax():

使用 $.ajax():

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success
  dataType: dataType
});

Or, before your post run $.ajaxSetup(), but this affects all ajax requests:

或者,在您发布之前运行 $.ajaxSetup(),但这会影响所有 ajax 请求:

$.ajaxSetup({
   beforeSend: myFunc,
   complete: myCompleteFunc
});

回答by kruonx

This will work for complete:

这将适用于完整:

var jqxhr = $.post("example.php", function() {
      alert("success");
jqxhr.complete(function(){ alert("second complete"); });

For beforeSend, you'll have to use $.ajaxSetup before calling $.post if you don't want to use $.ajax as they said before.

对于beforeSend,如果您不想像他们之前所说的那样使用$.ajax,则必须在调用$.post 之前使用$.ajaxSetup。

回答by Darin Dimitrov

You could use $.ajaxSetupbut it will apply globally. If this doesn't fit you you should use $.ajax.

您可以使用$.ajaxSetup但它会在全球范围内应用。如果这不适合你,你应该使用$.ajax

回答by Lior Cohen

Gotta use $.ajax, unless you use $.ajaxSetup(), but that may not be the wisest choice.

必须使用 $.ajax,除非您使用 $.ajaxSetup(),但这可能不是最明智的选择。

Any reason why you shouldn't use $.ajax?

你不应该使用 $.ajax 的任何理由?