1 2<!--- 3 4This README is automatically generated from the comments in these files: 5iron-ajax.html iron-request.html 6 7Edit those files, and our readme bot will duplicate them over here! 8Edit this file, and the bot will squash your changes :) 9 10The bot does some handling of markdown. Please file a bug if it does the wrong 11thing! https://github.com/PolymerLabs/tedium/issues 12 13--> 14 15[](https://travis-ci.org/PolymerElements/iron-ajax) 16 17_[Demo and API docs](https://elements.polymer-project.org/elements/iron-ajax)_ 18 19## Changes in 2.0 20 21* Promise polyfill is now a dev dependency and no longer shipped with `iron-ajax`. 22 23 `iron-ajax` uses the `Promise` API, which is not yet supported in all browsers. 24 25 The 1.x version of `iron-ajax` automatically loaded the promise polyfill. This 26 forced the application to include the polyfill, whether or not it was needed. 27 28 When using `iron-ajax` 2.x with Polymer 1.x, you must provide your own Promise polyfill, 29 if needed. For example, you could use the promise polyfill by installing it in your project: 30 31 bower install --save PolymerLabs/promise-polyfill#1 - 2 32 33 Then your app should include the promise polyfill before loading `iron-ajax`: 34 35 <link rel="import" href="bower_components/promise-polyfill/promise-polyfill-lite.html"> 36 37 You can use a different promise polyfill if you need a more fully-featured implementation of 38 Promise. 39 40 For Polymer 2.x, you **do not need to provide your own Promise polyfill if you are using 41 the web components polyfills.** Because the web components v1 APIs depend on `Promise`, 42 a promise polyfill is loaded when needed by the v1 polyfills (`web-components-lite.js` or 43 `webcomponents-loader.js`). 44 45* New optional error information. 46 47 The `generateRequest` method returns an `iron-request` element representing the 48 request, and the request element provides a `completes` property, which is a 49 promise that completes when the request either succeeds or fails. 50 51 This version includes a new flag, `rejectWithRequest`, that modifies the error handling 52 of the `completes` promise. By default, when the promise is rejected (because the request 53 failed), the rejection callback only receives an `Error` object describing the failure. 54 55 With `rejectWithRequest` set to true, the callback receives an object with two keys, `error`, 56 the error message, and `request`, the original request that the error is related to: 57 58 let request = ironAjaxElement.generateRequest(); 59 request.completes.then(function(req) { 60 // succesful request, argument is iron-request element 61 ... 62 }, function(rejected) { 63 // failed request, argument is an object 64 let req = rejected.request; 65 let error = rejected.error; 66 ... 67 } 68 ) 69 70 Because this change could break existing code, `rejectWithRequest` is false by default, 71 however, in the next major release, this option will be removed and the new behavior made 72 the default. 73 74 75## <iron-ajax> 76 77The `iron-ajax` element exposes network request functionality. 78 79```html 80<iron-ajax 81 auto 82 url="https://www.googleapis.com/youtube/v3/search" 83 params='{"part":"snippet", "q":"polymer", "key": "YOUTUBE_API_KEY", "type": "video"}' 84 handle-as="json" 85 on-response="handleResponse" 86 debounce-duration="300"></iron-ajax> 87``` 88 89With `auto` set to `true`, the element performs a request whenever 90its `url`, `params` or `body` properties are changed. Automatically generated 91requests will be debounced in the case that multiple attributes are changed 92sequentially. 93 94Note: The `params` attribute must be double quoted JSON. 95 96You can trigger a request explicitly by calling `generateRequest` on the 97element. 98 99 100 101## <iron-request> 102 103iron-request can be used to perform XMLHttpRequests. 104 105```html 106<iron-request id="xhr"></iron-request> 107... 108this.$.xhr.send({url: url, body: params}); 109``` 110 111 112