GM_xmlhttpRequest
From GreaseSpot
Contents |
[edit] Description
This method performs a similar function to the standard XMLHttpRequest object, but allows these requests to cross the same origin policy boundaries.
Compatibility: Greasemonkey 0.2.5+
[edit] Syntax
function GM_xmlhttpRequest( details )
[edit] Arguments
This method only takes one argument, the details object.
Described below are the properties that may be defined on that object.
See #Examples for more detail on how to use each.
-
method -
StringType of HTTP request to make (E.G."GET","POST") -
url -
StringThe URL to make the request to. Must be an absolute URL, beginning with the scheme. -
headers -
ObjectOptional. A set of headers to include in the request. [2] -
overrideMimeType -
String(Compatibility: 0.6.8+) Optional. A MIME type to specify with the request (E.G."text/html; charset=ISO-8859-1"). -
data -
StringOptional. Data to send in the request body. Usually forPOSTmethod requests. [1] -
binary -
Boolean(Compatibility: 0.8.3+) Optional, default false. When true, use the underlying.sendAsBinary()method. -
onerror -
FunctionOptional. Will be called when the request has completed successfully. Passed one argument, the #Response Object. -
onload -
FunctionOptional. Will be called when the request has completed successfully. Passed one argument, the #Response Object. -
onreadystatechange -
FunctionOptional. Will be called when the request has completed successfully. Passed one argument, the #Response Object.
[edit] Response Object
All three of the callback functions defined in the details object, if called, will receive this type of object as their first (and only) argument.
-
status -
IntegerThe HTTP response status (E.G. 200 or 404) upon success, ornullupon failure. -
statusText -
StringThe HTTP response status line (E.G."OK","Not Found") upon success, ornullupon failure. -
readyState -
NumberThereadyStateas defined in XMLHttpRequest. -
responseText -
StringTheresponseTextas defined in XMLHttpRequest. -
responseHeaders -
StringThe response headers as defined in XMLHttpRequest. -
finalUrl -
String(Compatibility: 0.8.0+) The final URL requested, ifLocationredirects were followed.
[edit] Returns
undefined
[edit] Examples
[edit] GET request
GM_xmlhttpRequest({
method: "GET",
url: "http://www.example.net/",
headers: {
"User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
"Accept": "text/xml" // If not specified, browser defaults will be used.
},
onload: function(response) {
// Inject responseXML into existing Object if not present
if (!response.responseXML) {
response.responseXML = new DOMParser()
.parseFromString(response.responseText, "text/xml");
}
GM_log([
response.status,
response.statusText,
response.readyState,
response.responseHeaders,
response.responseText,
response.finalUrl,
response.responseXML
].join("\n"));
}
});
[edit] POST request
When making a POST request, most sites require the Content-Type header to be defined as such:
GM_xmlhttpRequest({
method: "POST",
url: "http://www.example.net/login",
data: "username=johndoe&password=xyz123",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
if (response.responseText.indexOf("Logged in as") > -1) {
location.href = "http://www.example.net/dashboard";
}
}
});
[edit] HEAD request
As defined in HTTP, you may issue a HEAD request to read the response headers, but skip reading the entire response body. This request type must be supported by the server (but is by most).
GM_xmlhttpRequest({
url: "http://www.example.com",
method: "HEAD",
onload: function(response) {
GM_log(response.responseHeaders);
}
});
[edit] Notes
1
Note that if the data field contains form-encoded data, you usually must also set the header 'Content-Type': 'application/x-www-form-urlencoded' in the headers field.
2
Some headers may not actually work through GM_xmlhttpRequest.
For example, the Referer header cannot be overriden.
[1]
[2]

