using beforeSend and complete with $.post?

前端 未结 4 1284
轮回少年
轮回少年 2020-12-05 10:08

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

相关标签:
4条回答
  • 2020-12-05 10:44

    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.

    0 讨论(0)
  • 2020-12-05 10:45

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

    0 讨论(0)
  • 2020-12-05 10:45

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

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

    0 讨论(0)
  • 2020-12-05 11:02

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

    Using $.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({
       beforeSend: myFunc,
       complete: myCompleteFunc
    });
    
    0 讨论(0)
提交回复
热议问题