Gert Cuykens
Gert Cuykens

Reputation: 7155

iron-ajax response function

This works

        ready: function() {  
            this.response = function(e) {
                if (e.target.status == 200) {
                    this.data = e.target.response
                    this.query = ''
                }
            }.bind(this)
            var xhr = new XMLHttpRequest()
            xhr.open("GET", "acp.json", true)
            xhr.responseType = "json";
            xhr.setRequestHeader("Content-Type", "application/javascript");
            xhr.onload = this.response
            xhr.send()
        }

But I can't figure out how to bind the local this.query to a iron-ajax response like the above?

<iron-ajax auto url="acp.json" last-response="[[data]]" on-response="response"></iron-ajax>

Polymer({
  is:"acp-search",
  query:"i need this",
  response: function(e) {
    if (e.target.status == 200) {
      this.query = ''
    }
  },
  ready: function() {  

  }

Doesn't work, query is still undefined.

Upvotes: 2

Views: 1702

Answers (2)

mbunit
mbunit

Reputation: 483

e.target.status is undefined. Use e.detail.xhr.status

Upvotes: 3

Kevin Ashcraft
Kevin Ashcraft

Reputation: 581

For iron-ajax the on-response function is called when the status is 200 and the info is passed in as a property of the second variable.

response: function(e,d) {
  var myresponse = d.response;
  console.log("the response is", myresponse);
}

Upvotes: 1

Related Questions