Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the converter of $.ajax () in jQuery

Shulou Source: shulou.com Published: 2022-06-01 09:35:13 09月16日 Update

Most people do not understand the knowledge points of this article "how to use the converter of $.ajax () in jQuery", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "how to use the converter of $.ajax () in jQuery".

Starting with jQuery 1.5, jQuery's Ajax implementation includes pre-prefilters, transports and transport, giving you more flexibility to extend Ajax. For information about these advanced features, see Extending Ajax

Using Converters (using converters)

The data types supported by the converter of $.ajax () map to other data types. However, if you want to map a custom data type to a known type (json, etc.), you must add a relevant conversion function between the response Content-Type and the actual data type with the contents option:

$.ajaxSetup ({

Contents: {

Mycustomtype: / mycustomtype/

}

Converters: {

"mycustomtype json": function (result) {

/ / do stuff

Return newresult

}

}

})

This extra object is necessary because there is never a strict one-to-one correspondence between the response content type (Content-Types) and the data type (regular expressions represent the results).

Convert a supported type (such as text, json) to a custom data type, and then return, using another pass-through converter:

$.ajaxSetup ({

Contents: {

Mycustomtype: / mycustomtype/

}

Converters: {

"text mycustomtype": true

"mycustomtype json": function (result) {

/ / do stuff

Return newresult

}

}

})

Now the above code allows you to convert from text to mycustomtype, and then mycustomtype to json.

The above is about the content of this article on "how to use the converter of $.ajax () in jQuery". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Tags: Type converter content data article knowledge article support necessary one-to-one between code value information advanced function function most actual object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Shulou Tech Info Linux Xiaomi