OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of $LIBRARYNAME; | 5 part of $LIBRARYNAME; |
6 | 6 |
7 /** | 7 /** |
8 * A utility for retrieving data from a URL. | 8 * A utility for retrieving data from a URL. |
9 * | 9 * |
10 * HttpRequest can be used to obtain data from http, ftp, and file | 10 * HttpRequest can be used to obtain data from http, ftp, and file |
(...skipping 12 matching lines...) Expand all Loading... |
23 * with HttpRequest. However, there are ways to | 23 * with HttpRequest. However, there are ways to |
24 * [get around this restriction](http://www.dartlang.org/articles/json-web-servi
ce/#note-on-jsonp). | 24 * [get around this restriction](http://www.dartlang.org/articles/json-web-servi
ce/#note-on-jsonp). |
25 * | 25 * |
26 * See also: | 26 * See also: |
27 * | 27 * |
28 * * [Dart article on using HttpRequests](http://www.dartlang.org/articles/json-
web-service/#getting-data) | 28 * * [Dart article on using HttpRequests](http://www.dartlang.org/articles/json-
web-service/#getting-data) |
29 * * [JS XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpReq
uest) | 29 * * [JS XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpReq
uest) |
30 * * [Using XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttp
Request/Using_XMLHttpRequest) | 30 * * [Using XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttp
Request/Using_XMLHttpRequest) |
31 */ | 31 */ |
32 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 32 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 33 |
33 /** | 34 /** |
34 * Creates a URL get request for the specified `url`. | 35 * Creates a URL get request for the specified [url]. |
35 * | 36 * |
36 * After completing the request, the object will call the user-provided | 37 * The server response must be a `text/` mime type for this request to |
37 * [onComplete] callback. | 38 * succeed. |
| 39 * |
| 40 * This is similar to [request] but specialized for HTTP GET requests which |
| 41 * return text content. |
| 42 * |
| 43 * See also: |
| 44 * |
| 45 * * [request] |
38 */ | 46 */ |
39 factory $CLASSNAME.get(String url, onComplete($CLASSNAME request)) => | 47 static Future<String> getString(String url, |
40 _HttpRequestUtils.get(url, onComplete, false); | 48 {bool withCredentials, void onProgress(ProgressEvent e)}) { |
| 49 return request(url, withCredentials: withCredentials, |
| 50 onProgress: onProgress).then((xhr) => xhr.responseText); |
| 51 } |
41 | 52 |
42 // 80 char issue for comments in lists: dartbug.com/7588. | |
43 /** | 53 /** |
44 * Creates a URL GET request for the specified `url` with | 54 * Creates a URL request for the specified [url]. |
45 * credentials such a cookie (already) set in the header or | |
46 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2). | |
47 * | 55 * |
48 * After completing the request, the object will call the user-provided | 56 * By default this will do an HTTP GET request, this can be overridden with |
49 * [onComplete] callback. | 57 * [method]. |
50 * | 58 * |
51 * A few other details to keep in mind when using credentials: | 59 * The Future is completed when the response is available. |
| 60 * |
| 61 * The [withCredentials] parameter specified that credentials such as a cookie |
| 62 * (already) set in the header or |
| 63 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2) |
| 64 * should be specified for the request. Details to keep in mind when using |
| 65 * credentials: |
52 * | 66 * |
53 * * Using credentials is only useful for cross-origin requests. | 67 * * Using credentials is only useful for cross-origin requests. |
54 * * The `Access-Control-Allow-Origin` header of `url` cannot contain a wildca
rd (*). | 68 * * The `Access-Control-Allow-Origin` header of `url` cannot contain a wildca
rd (*). |
55 * * The `Access-Control-Allow-Credentials` header of `url` must be set to tru
e. | 69 * * The `Access-Control-Allow-Credentials` header of `url` must be set to tru
e. |
56 * * If `Access-Control-Expose-Headers` has not been set to true, only a subse
t of all the response headers will be returned when calling [getAllRequestHeader
s]. | 70 * * If `Access-Control-Expose-Headers` has not been set to true, only a subse
t of all the response headers will be returned when calling [getAllRequestHeader
s]. |
57 * | 71 * |
58 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access
_authentication). | 72 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access
_authentication). |
59 */ | 73 */ |
60 factory $CLASSNAME.getWithCredentials(String url, | 74 static Future<HttpRequest> request(String url, |
61 onComplete($CLASSNAME request)) => | 75 {String method, bool withCredentials, String responseType, sendData, |
62 _HttpRequestUtils.get(url, onComplete, true); | 76 void onProgress(ProgressEvent e)}) { |
| 77 var completer = new Completer<String>(); |
| 78 |
| 79 var xhr = new HttpRequest(); |
| 80 if (method == null) { |
| 81 method = 'GET'; |
| 82 } |
| 83 xhr.open(method, url, true); |
| 84 |
| 85 if (withCredentials != null) { |
| 86 xhr.withCredentials = withCredentials; |
| 87 } |
| 88 |
| 89 if (responseType != null) { |
| 90 xhr.responseType = responseType; |
| 91 } |
| 92 |
| 93 if (onProgress != null) { |
| 94 xhr.onProgress.listen(onProgress); |
| 95 } |
| 96 |
| 97 xhr.onLoad.listen((e) { |
| 98 if (xhr.status >= 200 && xhr.status < 300 || |
| 99 xhr.status == 304 ) { |
| 100 completer.complete(xhr); |
| 101 } else { |
| 102 completer.completeError(e); |
| 103 } |
| 104 }); |
| 105 |
| 106 xhr.onError.listen((e) { |
| 107 completer.completeError(e); |
| 108 }); |
| 109 |
| 110 if (sendData != null) { |
| 111 xhr.send(sendData); |
| 112 } else { |
| 113 xhr.send(); |
| 114 } |
| 115 |
| 116 return completer.future; |
| 117 } |
63 | 118 |
64 $!MEMBERS | 119 $!MEMBERS |
65 } | 120 } |
OLD | NEW |