Jquery Ajax beforeSend and success,error & complete

后端 未结 2 1061
小鲜肉
小鲜肉 2020-12-01 02:19

I have a problem with multiple ajax functions where the beforeSend of the second ajax post is executed before the complete function of the

2条回答
  •  既然无缘
    2020-12-01 02:41

    It's actually much easier with jQuery's promise API:

    $.ajax(
                type: "GET",
                url: requestURL,
            ).then((success) =>
                console.dir(success)
            ).failure((failureResponse) =>
                console.dir(failureResponse)
            )
    

    Alternatively, you can pass in of bind functions to each result callback; the order of parameters is: (success, failure). So long as you specify a function with at least 1 parameter, you get access to the response. So, for example, if you wanted to check the response text, you could simply do:

    $.ajax(
                type: "GET",
                url: @get("url") + "logout",
                beforeSend: (xhr) -> xhr.setRequestHeader("token", currentToken)
            ).failure((response) -> console.log "Request was unauthorized" if response.status is 401
    

提交回复
热议问题