GM.xmlHttpRequest: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (Undo revision 4135 by Aavindraa (Talk) Restored "sensical" statement)
(To eliminate confusion, remove any mention of return values, as there are none anyway.)
Line 18: Line 18:


:Value: Function
:Value: Function
:Returns: undefined, response Object
:Compatibility: [[Version_history#0.2.5|Greasemonkey 0.2.5+]]
:Compatibility: [[Version_history#0.2.5|Greasemonkey 0.2.5+]]


Line 83: Line 82:
==== onload ====
==== onload ====
:Usage: <code>''details''.'''onload''' = function (''response'') { ''/* some code */'' };</code>
:Usage: <code>''details''.'''onload''' = function (''response'') { ''/* some code */'' };</code>
:Returns: undefined, response Object


[[#top|top]] | [[#Syntax|back]]
[[#top|top]] | [[#Syntax|back]]
Line 89: Line 87:
==== onreadystatechange ====
==== onreadystatechange ====
:Usage: <code>''details''.'''onreadystatechange''' = function (''response'') { ''/* some code */'' };</code>
:Usage: <code>''details''.'''onreadystatechange''' = function (''response'') { ''/* some code */'' };</code>
:Returns: undefined, response Object


[[#top|top]] | [[#Syntax|back]]
[[#top|top]] | [[#Syntax|back]]
Line 95: Line 92:
==== onerror ====
==== onerror ====
:Usage: <code>''details''.'''onerror''' = function (''response'') { ''/* some code */'' };</code>
:Usage: <code>''details''.'''onerror''' = function (''response'') { ''/* some code */'' };</code>
:Returns: undefined, response Object


[[#top|top]] | [[#Syntax|back]]
[[#top|top]] | [[#Syntax|back]]

Revision as of 02:46, 15 September 2009

Template:Underscore


Greasemonkey Manual
Using Greasemonkey
Installing Scripts
Monkey Menu
Getting Help
User Script Authoring
Editing
Environment
API

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:

Examples | Notes

Syntax

GM_xmlhttpRequest( details )

Value: Function
Compatibility: Greasemonkey 0.2.5+
details Object
Properties Event Handler Callbacks
method onload
url onreadystatechange
headers onerror
overrideMimeType
data
  • All properties and event handler callbacks are optional except method and url.

top

Properties


method

Value: String
Usage: details.method = "GET"';

top | back

url

Value: String
Usage: details.url = "http://www.greasespot.net/";
  • Must be an absolute URL.

top | back

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]

top | back

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

top | back

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 the headers field.

top | back

Event Handler Callbacks


onload

Usage: details.onload = function (response) { /* some code */ };

top | back

onreadystatechange

Usage: details.onreadystatechange = function (response) { /* some code */ };

top | back

onerror

Usage: details.onerror = function (response) { /* some code */ };

top | back

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 of responseText using a DOMParser (see examples below).

top

Properties


status

Value: Number
Usage: if (response.status == 200) { /* some code */ }

top | back

statusText

Value: String
Usage: if (response.statusText == "OK") { /* some code */ }

top | back

readyState

Value: Number
Usage: if (response.readyState == 4) { /* some code */ }

top | back

responseText

Value: String
Usage: alert(response.responseText);

top | back

responseHeaders

Value: String
Usage: if (response.responseHeaders) { /* some code */ }
  • The responseHeaders is the string representation of response headers returned by XMLHTTPRequest.getAllResponseHeaders().

finalUrl

Value: String
Compatibility: Greasemonkey 0.8.0+
Usage: if (response.finalUrl) { /* some code */ }

top | back

Examples

Template:Core samp

When making a POST request, most sites require the Content-Type header to be included as such:

Template:Good samp

top

Notes

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

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.

top