GM.xmlHttpRequest: Difference between revisions
Line 49: | Line 49: | ||
:*Must be an absolute URL. | :*Must be an absolute URL. | ||
==== <code>headers</code> ==== | ==== <code>headers</code> ==== | ||
Line 57: | Line 57: | ||
:* The <code>headers</code> property is an object which is typically used to override the default browser generated headers, also known as atoms. It should contain the atom-value pairs of the headers to send.<sup>[[#Notes|[2]]]</sup> | :* The <code>headers</code> property is an object which is typically used to override the default browser generated headers, also known as atoms. It should contain the atom-value pairs of the headers to send.<sup>[[#Notes|[2]]]</sup> | ||
==== <code>overrideMimeType</code> ==== | ==== <code>overrideMimeType</code> ==== | ||
Line 65: | Line 65: | ||
:* While each character set name may have multiple aliases defined, there may be a preferred name which can be found at [http://www.iana.org/assignments/character-sets IANA] | :* While each character set name may have multiple aliases defined, there may be a preferred name which can be found at [http://www.iana.org/assignments/character-sets IANA] | ||
==== <code>data</code> ==== | ==== <code>data</code> ==== | ||
Line 79: | Line 79: | ||
:* Optional. Default false. If true, the underling xmlHttpRequest will have <tt>.sendAsBinary()</tt> instead of <tt>.send()</tt> called. | :* Optional. Default false. If true, the underling xmlHttpRequest will have <tt>.sendAsBinary()</tt> instead of <tt>.send()</tt> called. | ||
=== Event Handler Callbacks === | === Event Handler Callbacks === | ||
Line 87: | Line 87: | ||
:Returns: undefined, response Object | :Returns: undefined, response Object | ||
==== onreadystatechange ==== | ==== onreadystatechange ==== | ||
Line 93: | Line 93: | ||
:Returns: undefined, response Object | :Returns: undefined, response Object | ||
==== onerror ==== | ==== onerror ==== | ||
Line 99: | Line 99: | ||
:Returns: undefined, response Object | :Returns: undefined, response Object | ||
:{| cellpadding="5" style="border-style:solid; background:#FFFFE0;" | :{| cellpadding="5" style="border-style:solid; background:#FFFFE0;" | ||
|+ ''response'' Object | |+ ''response'' Object |
Revision as of 21:47, 3 February 2010
Description
This API method 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.
Additional reference may be found at:
Syntax
GM_xmlhttpRequest( details )
- Value: Function
- Returns: undefined, response Object
- Compatibility: Greasemonkey 0.2.5+
details Object Properties Event Handler Callbacks method
onload
url
onreadystatechange
headers
onerror
overrideMimeType
data
binary
Properties
method
- Value: String
- Usage:
details.method = "GET"';
url
- Value: String
- Usage:
details.url = "http://www.greasespot.net/";
- Must be an absolute URL.
headers
- Value: Object
- Usage:
details.headers = {"User-Agent":"Mozilla/5.0"};
- The
headers
property is an object which is typically used to override the default browser generated headers, also known as atoms. It should contain the atom-value pairs of the headers to send.[2]
- The
overrideMimeType
- Value: String
- Compatibility: Greasemonkey 0.6.8+
- Usage:
details.overrideMimeType = "text/html; charset=ISO-8859-1";
- While each character set name may have multiple aliases defined, there may be a preferred name which can be found at IANA
data
- Value: String
- Usage:
details.data = null; /* some code */ if (details.data) { /* some code */ }
- [1]
- Note that if the
data
field contains form-encoded data, you must also set the header'Content-Type': 'application/x-www-form-urlencoded'
in theheaders
field.
binary
- Value: Boolean
- Usage:
details.binary = true;
- Compatibility: Greasemonkey 0.8.3+
- Optional. Default false. If true, the underling xmlHttpRequest will have .sendAsBinary() instead of .send() called.
Event Handler Callbacks
onload
- Usage:
details.onload = function (response) { /* some code */ };
- Returns: undefined, response Object
onreadystatechange
- Usage:
details.onreadystatechange = function (response) { /* some code */ };
- Returns: undefined, response Object
onerror
- Usage:
details.onerror = function (response) { /* some code */ };
- Returns: undefined, response Object
response Object Properties status
readyState
responseText
finalUrl
statusText
responseHeaders
- Note: there is no
responseXML
property, contrary to what people used to XMLHttpRequest might expect. This is due to security restrictions imposed on the XMLDocument created in the privileged GM core context. One can easily create its own XMLDocument out ofresponseText
using a DOMParser (see examples below).
- Note: there is no
Properties
status
- Value: Number
- Usage:
if (response.status == 200) { /* some code */ }
statusText
- Value: String
- Usage:
if (response.statusText == "OK") { /* some code */ }
readyState
- Value: Number
- Usage:
if (response.readyState == 4) { /* some code */ }
responseText
- Value: String
- Usage:
alert(response.responseText);
responseHeaders
- Value: String
- Usage:
if (response.responseHeaders) { /* some code */ }
- The
responseHeaders
is the string representation of response headers returned byXMLHTTPRequest.getAllResponseHeaders()
.
- The
finalUrl
- Value: String
- Compatibility: Greasemonkey 0.8.0+
- Usage:
if (response.finalUrl) { /* some code */ }
Examples
GET request | POST request | HEAD request
Core
GET request
POST request
When making a POST request, most sites require the Content-Type header to be included as such:
HEAD request
Sometimes responseText
may be unnecessary, in which case a "HEAD" request is more advisable.
Template:Samp
Notes
[1]Some users have reported problems with international character sets. See these mailing list threads
- http://www.mozdev.org/pipermail/greasemonkey/2006-June/008785.html
- http://www.mozdev.org/pipermail/greasemonkey/2006-April/008064.html
Resolved: Greasemonkey exposed the overrideMimeType method to allow changing of the desired charset.
[2]Some header atoms may not actually work through GM_xmlhttpRequest. The Referer atom is one that is known to be recognized but appears to be content filtered by the browser. This may be due to security restrictions to keep user scripts from doing unsafe things.