Usages of jQuery's ajax crossDomain property?

后端 未结 6 722
悲哀的现实
悲哀的现实 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:41

    Lets assume , you have have another domain spanish.es2.com which serves spanish users of your website.

    You have the following requirement :

    1. Having a webpage on es2.com

    2. Call an api on es2.com and pass it some user info ( or cookie ), and get some user data. But if the user is spanish, the api on spanish.es2.com needs to be called for same data.

    3. When you do an ajax request with jQuery from es2.com to es2.com, for a spanish user :

      (a) With crossdomain disabled : Your es2.com api will find that the user is spanish, and hence do a http redirect to spanish.es2.com , which will not work due to ajax same domain policy and the ajax would fail. Redirects in ajax url -> Disallowed.

      (b) With crossdomain enabled : Your es2.com api's jsonp response,is actually loaded as a script tag wrapped in a function, hence a http redirect to other domain does not matter, and the content is still loaded, hence the ajax works. Redirects in src of a script tag -> Allowed.

    Hope this makes it clear.

提交回复
热议问题