GM.xmlHttpRequest: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
No edit summary
Line 10: Line 10:
This means that it '''is''' possible to issue requests to domains other than that of the current page.
This means that it '''is''' possible to issue requests to domains other than that of the current page.


The details parameter is an object having the following fields: method, url, headers, onload, onreadystatechange, onerror, data, overrideMimeType.
The details parameter is an object having the following properties: method, url, headers, onload, onreadystatechange, onerror, data, overrideMimeType.
All fields are optional except method and url.
All fields are optional except method and url.
The headers field is an object which should contain the name-value pairs of the headers to send, for instance {"User-Agent":"Mozilla"}.
The headers field is an object which should contain the name-value pairs of the headers to send, for instance {"User-Agent":"Mozilla"}.
The onload, onreadystatechange, and onerror parameters are callback functions which are called when the corresponding events occur.
The onload, onreadystatechange, and onerror parameters are callback functions which are called when the corresponding events occur.


Callback functions should accept a single object parameter having the following fields: responseText, status, statusText, readyState, and responseHeaders.
Callback functions should accept a single object parameter having the following properties: responseText, status, statusText, readyState, and responseHeaders.
The responseHeaders is the string representation of response headers returned by <code>XMLHTTPRequest.getAllResponseHeaders()</code>.
The responseHeaders is the string representation of response headers returned by <code>XMLHTTPRequest.getAllResponseHeaders()</code>.



Revision as of 13:24, 21 June 2007

Template:Underscore

Syntax

GM_xmlhttpRequest( details )

Description

This API provides access to the chrome-privileged XMLHttpRequest object. This means that it is possible to issue requests to domains other than that of the current page.

The details parameter is an object having the following properties: method, url, headers, onload, onreadystatechange, onerror, data, overrideMimeType. All fields are optional except method and url. The headers field is an object which should contain the name-value pairs of the headers to send, for instance {"User-Agent":"Mozilla"}. The onload, onreadystatechange, and onerror parameters are callback functions which are called when the corresponding events occur.

Callback functions should accept a single object parameter having the following properties: responseText, status, statusText, readyState, and responseHeaders. The responseHeaders is the string representation of response headers returned by XMLHTTPRequest.getAllResponseHeaders().

Examples

GM_xmlhttpRequest({
  method:"GET",
  url:"http://youngpup.net/",
  headers:{
    "User-Agent":"monkeyagent",
    "Accept":"text/monkey,text/xml",
  },
  onload:function(details) {
    alert([
      details.status,
      details.statusText,
      details.readyState,
      details.responseHeaders,
      details.responseText
    ].join("\n"))
  }
});

Notes

Some users have reported problems with international character sets. See these mailing list threads

The overrideMimeType parameter became available in version 0.6.8.