Usages of jQuery's ajax crossDomain property?

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

    The default for crossDomain is as follows:

    false for same-domain requests, true for crossDomain requests

    The data-type is interpreted differently depending on the value for the crossDomain setting:

    "json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options

    Because you are using jsonp instead of json you won't see any difference in your tests.

    When do I need to set the crossDomain property ?

    If you are making a same domain json request, and your site may redirect the request to another domain to serve the response (via HTTP 3XX), then you should set the crossDomain property to true so the response can be read by your calling script.

    This gives you the advantage of retrieving JSON when making same origin requests, and the functionality of JSONP when making cross-origin requests. If CORS is active on the domain you redirect to then you can set jsonp: false in the request options.

    Examples

    Making a request from example.com to example.org.

    • crossDomain automatically set to true.
    • Data type set to jsonp.

    Result: JSONP returned by example.org.

    Making a request from example.com to example.com.

    • crossDomain automatically set to false.
    • Data type set to jsonp.

    Result: JSONP returned by example.com.

    Making a request from example.com to example.org.

    • crossDomain automatically set to true.
    • Data type set to json.

    Result: JSONP returned by example.org.

    Making a request from example.com to example.com.

    • crossDomain automatically set to false.
    • Data type set to json.

    Result: JSON returned by example.com.

    Making a request from example.com to example.org.

    • crossDomain automatically set to true.
    • Data type set to json.
    • jsonp is set to false.
    • example.org does not support CORS for example.com

    Result: CORS error returned by browser.

    Making a request from example.com to example.com, example.com redirects AJAX to example.edu.

    • crossDomain manually set to true.
    • Data type set to json.

    Result: JSONP returned by example.edu.

    Making a request from example.com to example.org.

    • crossDomain automatically set to true.
    • Data type set to json.
    • jsonp is set to false.
    • example.org does support CORS for example.com

    Result: JSON returned by example.org.

    Making a request from example.com to example.com, example.com redirects AJAX to example.edu.

    • crossDomain automatically set to false.
    • Data type set to json.
    • example.edu does not support CORS for example.com

    Result: CORS error returned by browser.

提交回复
热议问题