Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 786
nnm Avatar asked May 18 '15 04:05

nnm


1 Answers

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>
like image 200
Neil John Ramal Avatar answered Sep 28 '22 12:09

Neil John Ramal