有没有人遇到过针对单个请求两次触发响应事件的铁-ajax?我已经仔细检查了,实际上我提交了一个请求。这是我的iron-ajax实现(只是包含iron-ajax的元素):
<dom-module id="my-ajax">
<template>
<iron-ajax id="ajax" auto="{{auto}}" url="{{url}}" method="{{method}}" headers="{{headers}}" body="{{body}}" handle-as="json" content-type="application/json" on-response="responseHandler" on-error="errorHandler" with-credentials></iron-ajax>
</template>
</dom-module>
// Register the polymer element
Polymer({
is: 'my-ajax',
properties: {
actionDesc: {type: String, value: ""},
auto: {type: Boolean, value: false},
body: {type: String, value: null},
headers: {type: Object, value: null},
isBusy: {
// One-way binding setup (i.e. child to host only)
type: Boolean,
value: false,
readOnly: true,
notify: true
},
method: {type: String, value: null},
user: {type: Object, value: null},
url: {type: String, value: null}
},
generateRequest: function() {
if (!this.isBusy) {
// Execute request as it isn't currently busy processing a previous request
this.isBusy = true;
this.$.ajax.generateRequest();
} else {
// TODO: Queue up this request
}
},
responseHandler: function(e, detail) {
console.log(this.id + " responseHandler fired!\n");
this.isBusy = false;
this.fire("handle-response", detail.xhr.response);
}
});
答案 0 :(得分:5)
<强>答案:强>
我认为您应该从auto
声明中删除<iron-ajax>
属性及其绑定,如下所示:
<iron-ajax id="ajax" url="{{url}}" method="{{method}}"
headers="{{headers}}" body="{{body}}" handle-as="json"
content-type="application/json"
on-response="responseHandler"
on-error="errorHandler" with-credentials></iron-ajax>
<小时/> 我认为问题出在“
auto
”属性中,Polymer´s documentation显示了这样的示例:
<iron-ajax
auto
url="http://gdata.youtube.com/feeds/api/videos/"
params='{"alt":"json", "q":"chrome"}'
handle-as="json"
on-response="handleResponse"
debounce-duration="300"></iron-ajax>
文档说:
auto
{Boolean
}默认:false
如果为true,则在url或params更改时自动执行Ajax请求。
所以,我认为当你添加“auto
”属性时,默认情况下它的值会自动设置为true,即使你绑定它也是如此。这就是为什么我认为你应该删除它。
对不起我的英语,我希望你能理解我。
答案 1 :(得分:0)
如Flavio Ochoa所述,auto
属性(如果为true)会使iron-ajax
自动执行请求 url 或 params 更改。
要使绑定按照您希望的方式工作而不进行两次调用,您的绑定应该$
=
auto
之前的<iron-ajax
id="ajax"
auto$="{{auto}}"
url="{{url}}"
method="{{method}}"
headers="{{headers}}"
body="{{body}}"
handle-as="json"
content-type="application/json"
on-response="responseHandler"
on-error="errorHandler"
with-credentials>
</iron-ajax>
,如下所示:
{{1}}
从Polymer docs开始,如果这样绑定的auto属性只会在绑定值为真的时设置
派对有点晚了,但希望这有助于某人