nnm
nnm

Reputation: 187

polymer iron-ajax : How to Bind data from input element to iron-ajax's body attribute

I recently have problems binding data from input element to iron-ajax's "body" attribute. When I used core-ajax on polymer 0.5, I can easily bind values like this:

<core-ajax
           id="ajax"
           method="POST" 
           contentType="application/json"
           url="{{url}}"   
           body='{"username":"{{username}}", "password":"{{password}}"}'
           handleAs="json"
           on-core-response="{{responseHandler}}">
</core-ajax>

Now I tried the same thing with iron-ajax. But it sends literally "{{username}}" and "{{password}}" instead of their values. Here is the code:

<iron-ajax
           id="ajax"
           method="POST" 
           contentType="application/json"
           url="{{url}}"   
           body='{"username":"{{username}}", "password":"{{password}}"}'
           handle-as="json"
           on-response="responseHandler">
</iron-ajax>

How to make it work? Thank you for your answers :)

Upvotes: 7

Views: 7188

Answers (2)

taylorstine
taylorstine

Reputation: 925

Another option is to use Computed Bindings

Your code would look something like this:

<iron-ajax
       ...
       body="{{getAjaxBody(username, password}}}"
       >
</iron-ajax>
<script>
Polymer({
  .....
  getAjaxBody: function(username, password) {
    return JSON.stringify({username: username, password: password});
  }
})
</script>

Upvotes: 0

Neil John Ramal
Neil John Ramal

Reputation: 3734

You can declare a computed property for the ajax body. Like so

properties: {
    ...
    ajaxBody: {
        type: String,
        computed: 'processBody(username, password)'
    }
},
processBody: function(username, password) {
    return JSON.stringify({username: username, password:password});
}

And then adding it on iron-ajax

<iron-ajax ... body="{{ajaxBody}}"></iron-ajax>

Upvotes: 5

Related Questions