[英]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. 我最近遇到了将输入元素的数据绑定到iron-ajax的“body”属性的问题。 When I used core-ajax on polymer 0.5, I can easily bind values like this:
当我在聚合物0.5上使用core-ajax时,我可以很容易地绑定这样的值:
<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. 现在我尝试用铁-ajax做同样的事情。 But it sends literally "{{username}}" and "{{password}}" instead of their values.
但它会逐字地发送“{{username}}”和“{{password}}”而不是其值。 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 :)
谢谢您的回答 :)
You can declare a computed property for the ajax body. 您可以声明ajax主体的计算属性。 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 然后将它添加到铁-ajax上
<iron-ajax ... body="{{ajaxBody}}"></iron-ajax>
Another option is to use Computed Bindings 另一种选择是使用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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.