Manual:CORS/fr

L'API MediaWiki supporte les requêtes CORS (cross-origin resource sharing) (en) si  est activé. C'est utile sur les sites Wikimedia, par exemple, pour permettre le téléversement d'images directement sur Commons depuis Wikipédia avec l'interface mobile.

Cette page détaille l'utilisation des requêtes CORS dans le code JavaScript pour communiquer entre les wikis de différents domaines.

Description
Pour que les requêtes CORS puissent transiter, vous devez fournir un paramètre  dans la chaîne de la requête avec comme valeur, le protocole et l'hôte du wiki local (par exemple,  ). Cela doit correspondre à l'une des valeurs de sur le wiki distant.

If you wish the browser to use any cookies it might have for the domain, to keep the current user logged in, you also need to set the  field of   to.

For anonymous requests,  query string parameter can be set to   which will allow requests from anywhere.

Exemples
In the examples below, we assume that the local wiki from which the requests originate is www.mediawiki.org, and that the foreign wiki which the requests target is en.wikipedia.org.

Using mediawiki.ForeignApi
The mediawiki.ForeignApi module is an extension of mediawiki.api which automatically handles the details for you.

An example that checks whether the user is logged in on the foreign wiki:

A basic write API example. We're acquiring a  token and using it to set a persistent custom user preference that a gadget might use afterwards:

The same example can be rewritten more succinctly using some mediawiki.api helper methods, which are available for ForeignApi too:

Using jQuery methods
If you do not wish to use mediawiki.api for whatever reason, or if you're curious how this works at a lower level, here's how to implement the same functionality using plain jQuery AJAX functionality. You could even use plain XMLHttpRequest.

An example that checks whether the user is logged in on the foreign wiki:

Un exemple de base d'API d'écriture : Notez que  est une partie de l'URL parce que les requêtes POST de CORS sont préarrangées et que le paramètres de l'origine doit être inclus dans la requête de préarrangement.

CentralAuth
CentralAuth allows your code to authenticate on the foreign wiki as the user currently logged in on the local wiki using a centralauthtoken. This guarantees that the same associated account will be used for actions on both wikis, unlike regular CORS (which requires the user to have previously logged in to the foreign wiki).

If both the local and foreign wiki have CentralAuth installed, the mediawiki.ForeignApi mechanism is seamlessly extended to handle this for you. If you're implementing it yourself, see centralauthtoken for instructions on how to acquire a token (from the local wiki) and pass it to a request (to the foreign wiki).

Alternatives
For anonymous requests you can use the JSONP format instead. This is simpler but slightly less secure (it fetches and executes arbitrary Javascript code from the wiki so an attacker who took over the MediaWiki site has an XSS vector against the remote site).