GM.xmlHttpRequest: Difference between revisions
From GreaseSpot Wiki
Jump to navigationJump to search
m Misc formatting and commenting |
m →onerror: Squished table to remove some whitespace |
||
Line 95: | Line 95: | ||
[[#top|top]] | [[#Syntax|back]] | [[#top|top]] | [[#Syntax|back]] | ||
:{| border="1" cellpadding="5" | :{| border="1" cellpadding="5" | ||
|+ | |+ responseDetails Object | ||
!|'''Properties | ! colspan="3" |'''Properties''' | ||
|- | |- | ||
|<code>[[#responseText |responseText]]</code> | |<code>[[#status |status]]</code> || <code>[[#readyState |readyState]]</code> || <code>[[#responseText |responseText]]</code> | ||
|- | |- | ||
|<code>[[#statusText |statusText]]</code> || || <code>[[#responseHeaders |responseHeaders]]</code> | |||
|<code>[[#statusText |statusText]]</code> | |||
| | |||
| | |||
|<code>[[#responseHeaders |responseHeaders]]</code> | |||
|} | |} | ||
Revision as of 07:41, 11 December 2007
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: Nothing, details Object
- Compatibility: Greasemonkey 0.2.5+
details Object Properties Event Handler Callbacks method
onload
url
onreadystatechange
headers
onerror
overrideMimeType
data
Properties
method
- Value: String
- Usage:
details.method = "GET"';
url
- Value: String
- Usage:
details.url = "http://www.greasespot.net/";
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 */ }
Event Handler Callbacks
onload
- Usage:
details.onload = function (response) { /* some code */ };
- Returns: Nothing, response Object
onreadystatechange
- Usage:
details.onreadystatechange = function (response) { /* some code */ };
- Returns: Nothing, response Object
onerror
- Usage:
details.onerror = function (response) { /* some code */ };
- Returns: Nothing, response Object
responseDetails Object Properties status
readyState
responseText
statusText
responseHeaders
Properties
responseText
- Value: String
- Usage:
alert(response.responseText);
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 */ }
responseHeaders
- Value: String
- Usage:
if (response.responseHeaders) { /* some code */ }
- The
responseHeaders
is the string representation of response headers returned byXMLHTTPRequest.getAllResponseHeaders()
.
- The
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"));
}
});
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
[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.