Get the App
SLTechnology News&Howtos  ›  Development  › 

How to modify text with jquery ajax

Shulou Source: shulou.com Published: 2022-06-01 07:35:44 10月22日 Update

In this article, the editor introduces in detail "how to use jquery ajax to modify the text", the content is detailed, the steps are clear, and the details are handled properly. I hope this "how to modify the text with jquery ajax" article can help you solve your doubts.

one. There are two ways to request an ajax:

Callback method:

The most common way to write, success and failure handling is passed in as a callback. [JavaScript] View copy code $.ajax in plain text ({

Ajax parameters...

Success: xxxxxx

Error: xxxxxx

})

Deferred mode:

The ajax call itself returns a Deferred object, and the success or failure callback is not passed in as a parameter. [JavaScript] View copy code $.ajax in plain text ({

Ajax parameters...

}). Then (function (res) {

/ / successfully processed the fragment

}, function (err) {

/ / failed processing fragment

})

Since there are these two ways, there are also two ways to deal with 401.

Callback method:

The processing in this way is relatively simple. 401 is judged in the failed callback. If so, authentication is performed and the request is resent successfully. [JavaScript] View copy code function getXXXX (type, url, data, success, error) {in plain text

$.ajax ({

Ajax parameters...

Success: xxxxxx

Error: function (xhr,textStatus,errorThrown) {

If (xhr.status==401) {

Refresh the authentication method (function () {

GetXXXX (type, url, data, success, error)

})

} else {

/ / call external error

Error & & error (xhr,textStatus,errorThrown)

}

}

})

}

Deferred mode:

In this way, the processing I have found so far requires modification of the jquery source code. [JavaScript] plain text view copy code / / Global setting a method

$.ajaxSetup ({

AuthError: function (callback) {

Refresh the authentication method (function () {

Callback & & callback ()

})

}

})

/ / jquery2.1.4 version source code, about 8261 lines

/ / Success/Error

If (isSuccess) {

Deferred.resolveWith (callbackContext, [success, statusText, jqXHR])

} else {

If ((jqXHR.status==401 | | jqXHR.status== 403) & & callbackContext.authError) {

CallbackContext.authError (function () {

State=0

JqXHR.setRequestHeader ("Authorization", XXXXXX)

JqXHR.readyState=1

Try {

State=1

Transport.send (requestHeaders, done)

} catch (e) {

/ / Propagate exception as error if not done

If (state < 2) {

Done (- 1, e)

/ / Simply rethrow otherwise

} else {

Throw e

}

}

})

Return

} else {

Deferred.rejectWith (callbackContext, [jqXHR, statusText, error])

}

}

Here's why you can't make a request in the first way:

(1). Then is chained so that the callback of the request is not in the parameter, but in an optionsCache global variable of jQuery.Callbacks. We cannot get the callback function in ajax error for retransmission.

(2). The callback written in then is destroyed once, and when error is triggered, it is destroyed after the callback is executed. The final approach is to intercept the 401 error before triggering the error, re-authenticate, and then reset the status and resend the request.

Read here, this "how to use jquery ajax to modify the text" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Tags: Mode processing text parameters success code identity identity authentication authentication article method global content that is is in source code fragment proper writing function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn MySQL Shulou Technology Apple