Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify text with jquery ajax

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report