Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymer 1.0 Iron-Ajax

Tags:

ajax

polymer

I am trying to get data via a php script that works in Polymer 0.5. I just get null response and no errors in Polymer 1.0, below is the code. I have tried modifying the PHP to echo anything but I get no response. hresponse does fire but at that point only the request information is in ajax the response information is null. I cannot find an example to see where I have gone wrong. Thanks

<iron-ajax
        id="ajax"
        url=""
        params=""
        handle-as="json"
        on-response="hresponse"
        debounce-duration="300">
</iron-ajax>

and the script

setajax:  function(){
   this.$.ajax.url = "Scripts/getnotes.php";
   this.$.ajax.params='{"SN":"VBA056"}';
   this.$.ajax.generateRequest();
}

hresponse: function(e) {
      console.log(e.detail.response);
      console.log(this.$.ajax.lastResponse);
}
like image 837
user2148935 Avatar asked Jun 01 '15 03:06

user2148935


People also ask

What is iron Ajax?

The iron-ajax element declaratively exposes network request functionality to Polymer's data-binding system.

What is polymer iron on?

Wear-Resistant Iron-On Labels These Iron-on labels are a Polymer alternative to the traditional vinyl labels and badges used on clothing. Polymer iron on labels are incredibly hardwearing and durable with no cracking when stretched and no fading after as many as 50 washes and iron's-on at just 120°.


1 Answers

When you add the this.$.ajax.params= inside the script, it should be an object. When you look into the place inside iron-ajax.html where the request is generated, you will see why this is the case. You are currently adding it as a String. Try to set the line to this.$.ajax.params={"SN":"VBA056"} and it should work.

The following example works (assuming you are importing all the required elements):

<body>
<my-app></my-app>
<dom-module id="my-app">
<style>
</style>
<template>

  <iron-ajax
      id="ajax"
      url=""
      handle-as="json"
      on-response="hresponse"
      debounce-duration="300">
  </iron-ajax>

  <button on-click="setajax">Click me</button>

</template>
<script>
Polymer({
  is: "my-app",
  setajax: function () {
    this.$.ajax.url = "http://jsonplaceholder.typicode.com/posts";
    this.$.ajax.params = {"userId":"1"};
    this.$.ajax.generateRequest();
  },
  hresponse: function(request) {
    console.log(request.detail.response);
    console.log(this.$.ajax.lastResponse);
  }
});
</script>
</dom-module>
</body>
like image 101
Erik Höhmann Avatar answered Sep 23 '22 17:09

Erik Höhmann