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 * Details to keep in mind when using credentials: | |
Emily Fortuna
2013/01/31 20:32:06
can you add back in lines 45-46 explaining what so
blois
2013/01/31 20:51:20
Done.
| |
52 * | 62 * |
53 * * Using credentials is only useful for cross-origin requests. | 63 * * Using credentials is only useful for cross-origin requests. |
54 * * The `Access-Control-Allow-Origin` header of `url` cannot contain a wildca rd (*). | 64 * * 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. | 65 * * 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]. | 66 * * 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 * | 67 * |
58 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access _authentication). | 68 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access _authentication). |
59 */ | 69 */ |
60 factory $CLASSNAME.getWithCredentials(String url, | 70 static Future<HttpRequest> request(String url, |
61 onComplete($CLASSNAME request)) => | 71 {String method, bool withCredentials, String responseType, sendData, |
62 _HttpRequestUtils.get(url, onComplete, true); | 72 void onProgress(ProgressEvent e)}) { |
73 var completer = new Completer<String>(); | |
74 | |
75 var xhr = new HttpRequest(); | |
76 if (method == null) { | |
77 method = 'GET'; | |
78 } | |
79 xhr.open(method, url, true); | |
80 | |
81 if (withCredentials != null) { | |
82 xhr.withCredentials = withCredentials; | |
83 } | |
84 | |
85 if (responseType != null) { | |
86 xhr.responseType = responseType; | |
87 } | |
88 | |
89 if (onProgress != null) { | |
90 xhr.onProgress.listen(onProgress); | |
91 } | |
92 | |
93 xhr.onLoad.listen((e) { | |
94 if (xhr.status >= 200 && xhr.status < 300 || | |
95 xhr.status == 304 ) { | |
96 completer.complete(xhr); | |
97 } else { | |
98 completer.completeError(e); | |
99 } | |
100 }); | |
101 | |
102 xhr.onError.listen((e) { | |
103 completer.completeError(e); | |
104 }); | |
105 | |
106 if (sendData != null) { | |
107 xhr.send(sendData); | |
108 } else { | |
109 xhr.send(); | |
110 } | |
111 | |
112 return completer.future; | |
113 } | |
63 | 114 |
64 $!MEMBERS | 115 $!MEMBERS |
65 } | 116 } |
OLD | NEW |