| 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 client-side XHR request for getting data from a URL, |
| 9 * | 9 * formally known as XMLHttpRequest. |
| 10 * HttpRequest can be used to obtain data from http, ftp, and file | 10 * |
| 11 * protocols. | 11 * HttpRequest can be used to obtain data from HTTP and FTP protocols, |
| 12 * | 12 * and is useful for AJAX-style page updates. |
| 13 * For example, suppose we're developing these API docs, and we | 13 * |
| 14 * wish to retrieve the HTML of the top-level page and print it out. | 14 * The simplest way to get the contents of a text file, such as a |
| 15 * The easiest way to do that would be: | 15 * JSON-formatted file, is with [getString]. |
| 16 * | 16 * For example, the following code gets the contents of a JSON file |
| 17 * HttpRequest.getString('http://api.dartlang.org').then((response) { | 17 * and prints its length: |
| 18 * print(response); | 18 * |
| 19 * }); | 19 * var path = 'myData.json'; |
| 20 * | 20 * HttpRequest.getString(path) |
| 21 * **Important**: With the default behavior of this class, your | 21 * .then((String fileContents) { |
| 22 * code making the request should be served from the same origin (domain name, | 22 * print(fileContents.length); |
| 23 * port, and application layer protocol) as the URL you are trying to access | 23 * }) |
| 24 * with HttpRequest. However, there are ways to | 24 * .catchError((Error error) { |
| 25 * [get around this restriction](http://www.dartlang.org/articles/json-web-servi
ce/#note-on-jsonp). | 25 * print(error.toString()); |
| 26 * | 26 * }); |
| 27 * See also: | 27 * |
| 28 * | 28 * ## Fetching data from other servers |
| 29 * * [Dart article on using HttpRequests](http://www.dartlang.org/articles/json-
web-service/#getting-data) | 29 * |
| 30 * * [JS XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpReq
uest) | 30 * For security reasons, browsers impose restrictions on requests |
| 31 * * [Using XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttp
Request/Using_XMLHttpRequest) | 31 * made by embedded apps. |
| 32 * With the default behavior of this class, |
| 33 * the code making the request must be served from the same origin |
| 34 * (domain name, port, and application layer protocol) |
| 35 * as the requested resource. |
| 36 * In the example above, the myData.json file must be co-located with the |
| 37 * app that uses it. |
| 38 * You might be able to |
| 39 * [get around this restriction](http://www.dartlang.org/articles/json-web-serv
ice/#a-note-on-cors-and-httprequest) |
| 40 * by using CORS headers or JSONP. |
| 41 * |
| 42 * ## Other resources |
| 43 * |
| 44 * * [Fetch Data Dynamically](https://www.dartlang.org/docs/tutorials/fetchdata
/), |
| 45 * a tutorial from _A Game of Darts_, |
| 46 * shows two different ways to use HttpRequest to get a JSON file. |
| 47 * * [Get Input from a Form](https://www.dartlang.org/docs/tutorials/forms/), |
| 48 * another tutorial from _A Game of Darts_, |
| 49 * shows using HttpRequest with a custom server. |
| 50 * * [Dart article on using HttpRequests](http://www.dartlang.org/articles/json
-web-service/#getting-data) |
| 51 * * [JS XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRe
quest) |
| 52 * * [Using XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHtt
pRequest/Using_XMLHttpRequest) |
| 32 */ | 53 */ |
| 33 $(ANNOTATIONS)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 54 $(ANNOTATIONS)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 34 | 55 |
| 35 /** | 56 /** |
| 36 * Creates a URL get request for the specified [url]. | 57 * Creates a GET request for the specified [url]. |
| 37 * | 58 * |
| 38 * The server response must be a `text/` mime type for this request to | 59 * The server response must be a `text/` mime type for this request to |
| 39 * succeed. | 60 * succeed. |
| 40 * | 61 * |
| 41 * This is similar to [request] but specialized for HTTP GET requests which | 62 * This is similar to [request] but specialized for HTTP GET requests which |
| 42 * return text content. | 63 * return text content. |
| 43 * | 64 * |
| 44 * See also: | 65 * See also: |
| 45 * | 66 * |
| 46 * * [request] | 67 * * [request] |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 headers[key] = '${headers[key]}, $value'; | 322 headers[key] = '${headers[key]}, $value'; |
| 302 } else { | 323 } else { |
| 303 headers[key] = value; | 324 headers[key] = value; |
| 304 } | 325 } |
| 305 } | 326 } |
| 306 return headers; | 327 return headers; |
| 307 } | 328 } |
| 308 | 329 |
| 309 $!MEMBERS | 330 $!MEMBERS |
| 310 } | 331 } |
| OLD | NEW |