how does jquery guess the better datatype in Ajax method?

后端 未结 2 1807
栀梦
栀梦 2021-01-18 03:55

it is wriiten in jquery document about Ajax method and its dataType:

\' Default: Intelligent Guess \'

how does jquery guess the better datatype for this meth

相关标签:
2条回答
  • 2021-01-18 04:30

    Don't rely on jQuery to correctly guess your returned dataType. I was returning text but I was foolishly assuming that text was the default setting without looking at the documentation and my ajax requests would seemingly randomly fail, giving some of my users a bad experience and others would be fine.

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

    0 讨论(0)
  • Primarily by looking at the Content-Type header of the response. Details in the ajaxHandleResponses function in the source (currently, for v1.5.2, starting at line 6,932).

    From the documentation:

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

    Re

    if the output of requested url via ajax has html and script both,which one will be consider? html or script as dataType?

    That would be HTML with embedded script tags, which are also HTML. The script tags will be evaluated when (if) you insert the HTML into the DOM. Example: http://jsbin.com/utuha3

    0 讨论(0)
提交回复
热议问题