API:Cross-site requests

From MediaWiki.org
Jump to: navigation, search

Other languages:
català • ‎Deutsch • ‎English • ‎español • ‎français • ‎Հայերեն • ‎italiano • ‎日本語 • ‎Bahasa Melayu • ‎Nederlands • ‎polski • ‎português • ‎português do Brasil • ‎română • ‎русский • ‎sicilianu • ‎中文
This page is part of the MediaWiki action API documentation.

MediaWiki action API

v · d · e

If a user script or gadget needs to make an API call against another MediaWiki site (e.g. a script on the English Wikipedia needs to check image information on Commons), it must use JSONP or CORS.

JSONP usage[edit]

The API format=json accepts a "callback" parameter, which names a JavaScript function in which the JSON result will be wrapped. This may be used to call the API on a remote site by dynamically adding <script> tags to the document.

Note that any JSONP requests will be processed as if logged out, even if the browser session is authenticated against the remote wiki.

CORS usage[edit]

The MediaWiki API requires that the origin be supplied as a request parameter, appropriately named "origin", which is matched against the Origin header required by the CORS protocol. Note that this parameter must be included in any pre-flight request, and so should be included in the query string portion of the request URI even for POST requests.

To make an authenticated CORS request, the remote wiki's $wgCrossSiteAJAXdomainsManual:$wgCrossSiteAJAXdomains setting must be set to allow the origin site. If the CORS origin check passes, MediaWiki will include the Access-Control-Allow-Credentials: true header in the response, so authentication cookies may be sent.

Unauthenticated CORS requests may be made from any origin by setting the "origin" request parameter to "*". In this case MediaWiki will include the Access-Control-Allow-Credentials: false header in the response and will process the request as if logged out (in case credentials are somehow sent anyway).

When the "origin" request parameter is supplied, MediaWiki (since 1.30) will return a MediaWiki-CORS-Rejection header with a brief failure reason if the request does not result in a successful CORS response, e.g. in case of mismatched origin or unsupported headers in a Access-Control-Request-Headers request header.

See Manual: CORS for instructions on how to handle CORS requests in JavaScript.

See also[edit]