googlearchive / core-ajax

Polymer Core Ajax
26 stars 52 forks source link

[0.5] Can't do POST requests - body attribute breaks #94

Closed grvcoelho closed 9 years ago

grvcoelho commented 9 years ago

Hello there folks, I have the following situation:

Inside an element, I have this core-ajax element:


<polymer-element name="my-element">
  <template>
    <core-ajax
          id="ajax"
          url="/test"
          method="POST"
          handleAs="json"
          body="{{newContact}}"
          on-core-response="{{handleResponse}}">
    </core-ajax>
  </template>

  <script>
    Polymer('my-element', {
      created: function() {
        this.newContact = {
          name: 'John Doe',
          email: 'john@doe.com',
          phone: '555 3211'
        }
      },

      handleResponse: function(event, detail, sender) {
        console.info(detail.response);
      }
    });
  </script>
</polymer-element>

And I have an express server setup just to return the body of the POST message:

app.post('/test', function(req, res) {
  res.json(req.body)
});

However, the answer from the server I get is:

Object {object Object: ""}

wizawu commented 9 years ago

Refer to https://www.polymer-project.org/0.5/docs/elements/core-ajax.html

With auto set to true, the element performs a request whenever its url, params or body properties are changed.

It's the first solution. Or you can try

newContactChanged: function() {
  this.$.ajax.go()
}
grvcoelho commented 9 years ago

I've tried that too @wizawu, but it didn't work to me.

But I used the params attribute instead of the body attribute, and it worked. :smile: