简体   繁体   中英

Polymer 1.0 Iron-Ajax

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);
}

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>

My 1st time answering and a newbie to programming, so it is not perfectly correct. But it can help you

There is no property as on-response as per documentation provided in https://elements.polymer-project.org/elements/iron-ajax

Your iron-ajax has to be modified, i too am a newbie, so i wonder if it works, but worth a try.

<iron-ajax
        auto
        url=""
        params=""
        handleAs="json"
        lastResponse="hresponse"
        method='GET'>
</iron-ajax>

Also i wonder if your scripts work like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM