Usages of jQuery's ajax crossDomain property?

后端 未结 6 720
悲哀的现实
悲哀的现实 2021-02-02 10:19

According to jQuery :

crossDomain (default: false for same-domain requests, true for cross-domain requests) Type: Boolean If you wish to f

6条回答
  •  情歌与酒
    2021-02-02 10:30

    It isn't default false when missing. When missing it defaults to true if the domains are not the same (as in your first sample above). I think you can leave it to its default value in nearly if not all cases.

    Also, when setting the cross-domain parameter, JQuery attempts to use CORS by default, not JSONP.

    Here are some relevant snippets from JQuery source: https://github.com/jquery/jquery/blob/master/src/ajax/xhr.js

    variable "xhrSupported"...

    xhrSupported = jQuery.ajaxSettings.xhr();
    

    ..is used to check for CORS support....

    support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
    

    .. which is checked when making AJAX calls....

    jQuery.ajaxTransport(function( options ) {
            var callback;
    
            // Cross domain only allowed if supported through XMLHttpRequest
            if ( support.cors || xhrSupported && !options.crossDomain ) 
    

    Hope this helps!

提交回复
热议问题