admin管理员组文章数量:1321449
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 :)
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 :)
Share Improve this question edited May 18, 2015 at 4:45 TimoStaudinger 42.5k16 gold badges89 silver badges96 bronze badges asked May 18, 2015 at 4:41 nnmnnm 1872 silver badges9 bronze badges2 Answers
Reset to default 5You can declare a puted property for the ajax body. Like so
properties: {
...
ajaxBody: {
type: String,
puted: '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>
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>
版权声明:本文标题:javascript - polymer iron-ajax : How to Bind data from input element to iron-ajax's body attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742099864a2420754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论