HttpRequest class

A client-side XHR request for getting data from a URL, formally known as XMLHttpRequest.

HttpRequest can be used to obtain data from HTTP and FTP protocols, and is useful for AJAX-style page updates.

The simplest way to get the contents of a text file, such as a JSON-formatted file, is with getString. For example, the following code gets the contents of a JSON file and prints its length:

var path = 'myData.json';
HttpRequest.getString(path)
    .then((String fileContents) {
      print(fileContents.length);
    })
    .catchError((Error error) {
      print(error.toString());
    });

Fetching data from other servers

For security reasons, browsers impose restrictions on requests made by embedded apps. With the default behavior of this class, the code making the request must be served from the same origin (domain name, port, and application layer protocol) as the requested resource. In the example above, the myData.json file must be co-located with the app that uses it. You might be able to get around this restriction by using CORS headers or JSONP.

Other resources

Inheritance
Annotations
  • @DomName('XMLHttpRequest')

Constants

DONEint
@DocsEditable(), @DomName('XMLHttpRequest.DONE')
4
HEADERS_RECEIVEDint
@DocsEditable(), @DomName('XMLHttpRequest.HEADERS_RECEIVED')
2
LOADINGint
@DocsEditable(), @DomName('XMLHttpRequest.LOADING')
3
OPENEDint
@DocsEditable(), @DomName('XMLHttpRequest.OPENED')
1
readyStateChangeEventEventStreamProvider<ProgressEvent>
@DocsEditable(), @DomName('XMLHttpRequest.readystatechangeEvent')

Static factory designed to expose readystatechange events to event handlers that are not necessarily instances of HttpRequest.

const EventStreamProvider<ProgressEvent>('readystatechange')
UNSENTint
@DocsEditable(), @DomName('XMLHttpRequest.UNSENT')
0

Static Properties

instanceRuntimeTypeType
@Deprecated("Internal Use Only"), read-only
supportsCrossOriginbool
read-only

Checks to see if the current platform supports making cross origin requests.

supportsLoadEndEventbool
read-only

Checks to see if the LoadEnd event is supported on the current platform.

supportsOverrideMimeTypebool
read-only

Checks to see if the overrideMimeType method is supported on the current platform.

supportsProgressEventbool
read-only

Checks to see if the Progress event is supported on the current platform.

Static Methods

getString(String url, { bool withCredentials, void onProgress(ProgressEvent e) }) → Future<String>

Creates a GET request for the specified url.

postFormData(String url, Map<String, String> data, { bool withCredentials, String responseType, Map<String, String> requestHeaders, void onProgress(ProgressEvent e) }) → Future<HttpRequest>

Makes a server POST request with the specified data encoded as form data.

request(String url, { String method, bool withCredentials, String responseType, String mimeType, Map<String, String> requestHeaders, sendData, void onProgress(ProgressEvent e) }) → Future<HttpRequest>

Creates and sends a URL request for the specified url.

requestCrossOrigin(String url, { String method, String sendData }) → Future<String>
@Experimental()

Makes a cross-origin request to the specified URL.

Constructors

HttpRequest()
factory

General constructor for any type of request (GET, POST, etc).

HttpRequest.internal_()

Properties

onReadyStateChangeStream<ProgressEvent>
@DocsEditable(), @DomName('XMLHttpRequest.onreadystatechange'), read-only

Event listeners to be notified every time the HttpRequest object's readyState changes values.

readyStateint
@DocsEditable(), @DomName('XMLHttpRequest.readyState'), read-only

Indicator of the current state of the request:

responseObject
@DocsEditable(), @DomName('XMLHttpRequest.response'), @SupportedBrowser(SupportedBrowser.CHROME), @SupportedBrowser(SupportedBrowser.FIREFOX), @SupportedBrowser(SupportedBrowser.IE, '10'), @SupportedBrowser(SupportedBrowser.SAFARI), read-only

The data received as a reponse from the request.

responseHeadersMap<String, String>
read-only

Returns all response headers as a key-value map.

responseTextString
@DocsEditable(), @DomName('XMLHttpRequest.responseText'), read-only

The response in String form or empty String on failure.

responseTypeString
@DocsEditable(), @DomName('XMLHttpRequest.responseType'), read / write

String telling the server the desired response format.

responseUrlString
@DocsEditable(), @DomName('XMLHttpRequest.responseURL'), @Experimental(), read-only
responseXmlDocument
@DocsEditable(), @DomName('XMLHttpRequest.responseXML'), read-only

The request response, or null on failure.

statusint
@DocsEditable(), @DomName('XMLHttpRequest.status'), read-only

The HTTP result code from the request (200, 404, etc). See also: HTTP Status Codes

statusTextString
@DocsEditable(), @DomName('XMLHttpRequest.statusText'), read-only

The request response string (such as "200 OK"). See also: HTTP Status Codes

timeoutint
@DocsEditable(), @DomName('XMLHttpRequest.timeout'), @Experimental(), read / write

Length of time in milliseconds before a request is automatically terminated.

uploadHttpRequestUpload
@DocsEditable(), @DomName('XMLHttpRequest.upload'), @Unstable(), read-only

EventTarget that can hold listeners to track the progress of the request. The events fired will be members of HttpRequestUploadEvents.

withCredentialsbool
@DocsEditable(), @DomName('XMLHttpRequest.withCredentials'), read / write

True if cross-site requests should use credentials such as cookies or authorization headers; false otherwise.

hashCodeint
read-only, inherited
onEvents
read-only, inherited

This is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.

onAbortStream<ProgressEvent>
@DocsEditable(), @DomName('XMLHttpRequestEventTarget.onabort'), @Experimental(), read-only, inherited

Stream of abort events handled by this HttpRequestEventTarget.

onErrorStream<ProgressEvent>
@DocsEditable(), @DomName('XMLHttpRequestEventTarget.onerror'), @Experimental(), read-only, inherited

Stream of error events handled by this HttpRequestEventTarget.

onLoadStream<ProgressEvent>
@DocsEditable(), @DomName('XMLHttpRequestEventTarget.onload'), @Experimental(), read-only, inherited

Stream of load events handled by this HttpRequestEventTarget.

onLoadEndStream<ProgressEvent>
@DocsEditable(), @DomName('XMLHttpRequestEventTarget.onloadend'), @Experimental(), @SupportedBrowser(SupportedBrowser.CHROME), @SupportedBrowser(SupportedBrowser.FIREFOX), @SupportedBrowser(SupportedBrowser.IE, '10'), @SupportedBrowser(SupportedBrowser.SAFARI), read-only, inherited

Stream of loadend events handled by this HttpRequestEventTarget.

onLoadStartStream<ProgressEvent>
@DocsEditable(), @DomName('XMLHttpRequestEventTarget.onloadstart'), @Experimental(), read-only, inherited

Stream of loadstart events handled by this HttpRequestEventTarget.

onProgressStream<ProgressEvent>
@DocsEditable(), @DomName('XMLHttpRequestEventTarget.onprogress'), @Experimental(), @SupportedBrowser(SupportedBrowser.CHROME), @SupportedBrowser(SupportedBrowser.FIREFOX), @SupportedBrowser(SupportedBrowser.IE, '10'), @SupportedBrowser(SupportedBrowser.SAFARI), read-only, inherited

Stream of progress events handled by this HttpRequestEventTarget.

onTimeoutStream<ProgressEvent>
@DocsEditable(), @DomName('XMLHttpRequestEventTarget.ontimeout'), @Experimental(), read-only, inherited

Stream of timeout events handled by this HttpRequestEventTarget.

runtimeTypeType
read-only, inherited

A representation of the runtime type of the object.

Operators

operator ==(other) → bool
inherited

The equality operator.

Methods

abort() → void
@DocsEditable(), @DomName('XMLHttpRequest.abort')

Stop the current request.

getAllResponseHeaders() → String
@DocsEditable(), @DomName('XMLHttpRequest.getAllResponseHeaders'), @Unstable()

Retrieve all the response headers from a request.

getResponseHeader(String name) → String
@DocsEditable(), @DomName('XMLHttpRequest.getResponseHeader'), @Unstable()

Return the response header named header, or null if not found.

open(String method, String url, { bool async, String user, String password }) → void
@DocsEditable(), @DomName('XMLHttpRequest.open')

Specify the desired url, and method to use in making the request.

overrideMimeType(String mime) → void
@DocsEditable(), @DomName('XMLHttpRequest.overrideMimeType'), @SupportedBrowser(SupportedBrowser.CHROME), @SupportedBrowser(SupportedBrowser.FIREFOX), @SupportedBrowser(SupportedBrowser.SAFARI)

Specify a particular MIME type (such as text/xml) desired for the response.

send([body_OR_data ]) → void
setRequestHeader(String name, String value) → void
@DocsEditable(), @DomName('XMLHttpRequest.setRequestHeader')

Sets the value of an HTTP request header.

addEventListener(String type, EventListener listener, [ bool useCapture ]) → void
inherited
dispatchEvent(Event event) → bool
@DocsEditable(), @DomName('EventTarget.dispatchEvent'), inherited
noSuchMethod(Invocation invocation) → dynamic
inherited

Invoked when a non-existent method or property is accessed.

removeEventListener(String type, EventListener listener, [ bool useCapture ]) → void
inherited
toString() → String
inherited

Returns the result of the JavaScript objects toString method.

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-html/HttpRequest-class.html