How does jquery guess the best data type in an Ajax method?

The jquery document contains a description of the Ajax method and its dataType:

'Default: Intelligent Guess'

how does jquery guess the best data type for this method if nothing is selected for it?

if at the output of the requested url via ajax there is html and script both to be considered? html or script like dataType?

+6
source share
2 answers

First of all, by looking at the Content-Type header of the response. Details in ajaxHandleResponses in the source (currently for version 1.5.2, starting from line 6, 932).

From the documentation :

dataType : ... If none is specified, jQuery will try to output it based on the MIME response type (the XML MIME type will give XML, the JavaScript object will be released in 1.4 JSON, the script will execute in 1.4 script, and everything else will be returned as a string).

Re

if at the output of the requested url via ajax there is html and script both to be considered? html or script like dataType?

This will be HTML with embedded script tags, which are also HTML. script tags will be evaluated when (if) you embed HTML in the DOM. Example: http://jsbin.com/utuha3

+8
source

Do not rely on jQuery to correctly guess the returned dataType . I returned text , but I foolishly assumed that text was the default setting without looking at the documentation , and my ajax requests seem to be random failing, giving some of my users a bad experience, and others will be fine.

i.e. jQuery cannot reliably intelligently guess text as dataType .

+1
source

Source: https://habr.com/ru/post/886683/


All Articles