<iron-ajax>元素用于进行Ajax调用。
句法:
<iron-ajax
auto url = "https://www.googleapis.com/youtube/v3/search"
params = '{"part":"snippet", "q":"polymer", "key": "YOUTUBE_API_KEY", "type": "video"}'
handle-as = "json"
on-response = "handleResponse"
debounce-duration = "500">
</iron-ajax>
如果将auto设置为true, 则每次更改元素的属性url, params或body时, 该元素都会发出请求。
参数说明
url:它指定将URL放置到API端点的属性。
params:它指定一个属性, 你可以在其中传递带有请求参数的JSON。
handle-as:它指定必须在响应属性中存储哪些数据。默认情况下, 数据以JSON格式存储。
on-response:它指定一个属性, 该属性可以通过处理响应的方法来告诉iron-ajax组件。
顺序更改多个属性会导致自动生成的请求被反跳。
你可以在元素上调用generateRequest以显式触发请求。
例:
<!DOCTYPE html>
<html>
<head>
<title>iron-ajax</title>
<link rel = "import"
href = "https://cdn.rawgit.com/download/polymer-cdn/1.0.1/lib/paper-input/paper-input.html">
<link rel = "import"
href = "https://cdn.rawgit.com/download/polymer-cdn/1.0.1/lib/iron-ajax/iron-ajax.html">
</head>
<body>
<dom-module id = "data-bind">
<template>
<paper-input label = "Write something..." id = "input" value = "{{q::input}}"
autofocus>
</paper-input>
<p>You typed: <b>{{q}}</b></p>
<p>Echo: <b>{{echo}}</b></p>
<iron-ajax auto
url = "http://dict.leo.org/dictQuery/m-query/conf/ende/query.conf/strlist.json"
params = "{'q': q}" handle-as = "json" on-response = "_handleResponse"
debounce-duration = "500">
</iron-ajax>
</template>
<script>
(function () {
Polymer ({
is: 'data-bind', properties: {
echo: {
type: String, value: 'waiting ...', reflectToAttribute: true
}
}, _handleResponse: function(event, request) {
var response = request.response;
this.echo = JSON.stringify(response);
}
});
})();
</script>
</dom-module>
<data-bind></data-bind>
</body>
</html>
输出
评论前必须登录!
注册