iron-ajax(聚合物1.0)响应事件发射两次

sin*_*ins 3 polymer polymer-1.0

有没有人遇到过针对单个请求两次触发的铁-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>
Run Code Online (Sandbox Code Playgroud)

// 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);
    }
});
Run Code Online (Sandbox Code Playgroud)

小智 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>
Run Code Online (Sandbox Code Playgroud)


我认为问题出在" auto"属性,Polymer的文档中显示了这样一个例子:

<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>
Run Code Online (Sandbox Code Playgroud)

文档说:

auto{ Boolean}默认值:false

如果为true,则在url或params更改时自动执行Ajax请求.

所以,我想当你添加"auto "属性时,默认情况下它的值会自动设置为true,即使你绑定它也是如此.这就是我认为你应该删除它的原因.

抱歉我的英语,我希望你能理解我.