GM.xmlHttpRequest: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
Marti (talk | contribs)
Marti (talk | contribs)
m →‎Notes: More backrefs
Line 165: Line 165:


= Notes =
= Notes =
<sup>[1]</sup>Some users have reported problems with international character sets.
<sup>[[GM_xmlhttpRequest#data|[1]]]</sup>Some users have reported problems with international character sets.
See these [[mailing list]] threads
See these [[mailing list]] threads


Line 171: Line 171:
* http://www.mozdev.org/pipermail/greasemonkey/2006-April/008064.html
* http://www.mozdev.org/pipermail/greasemonkey/2006-April/008064.html


<sup>[2]</sup>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.
<sup>[[GM_xmlhttpRequest#headers|[2]]]</sup>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.
* http://groups.google.com/group/greasemonkey-dev/browse_thread/thread/77c94cc17c6b2669
* http://groups.google.com/group/greasemonkey-dev/browse_thread/thread/77c94cc17c6b2669
* http://userscripts.org/forums/1/topics/1302?page=1#posts-5312
* http://userscripts.org/forums/1/topics/1302?page=1#posts-5312

Revision as of 02:25, 7 December 2007

Template:Underscore


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 )

Returns: Nothing, details Object
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/"';

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 = "application/xhtml+xml";

top | back

data

Value: String
Usage: details.data = null;

top | back

Event Handler Callbacks


onload

Usage: details.onload = function (response) { // Some code };
Returns: Nothing, response Object

top | back

onreadystatechange

Usage: details.onreadystatechange = function (response) { // Some code };
Returns: Nothing, response Object

top | back

onerror

Usage: details.onerror = function (response) { // Some code };
Returns: Nothing, response Object

top | back

response Object
Properties Event Handler Callbacks
responseText
status
statusText
readyState
responseHeaders

Properties


responseText

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

top | back

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

responseHeaders

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

top | back

Examples

GM_xmlhttpRequest({
  method:"GET",
  url:"http://www.greasespot.net/",
  headers:{
    "User-Agent":"Mozilla/5.0",            // Recommend using navigator.userAgent when possible
    "Accept":"text/xml"
  },
  onload:function(response) {
    alert([
      response.status,
      response.statusText,
      response.readyState,
      response.responseHeaders,
      response.responseText
    ].join("\n"));
  }
});

top

Notes

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

[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