聚合物铁AJAX:如何绑定数据输入元素铁阿贾克斯的身体属性聚合物、绑定、属性、元素

2023-09-10 17:34:05 作者:初与友歌

我最近有问题,从输入元素将数据绑定到铁阿贾克斯的身体的属性。 当我使用的核心AJAX聚合物0.5,我可以很容易地将值绑定是这样的:

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>

现在我想同样的事情用铁阿贾克斯。但它发出字面意思是{{用户名}},而不是和{{密码}}他们的价值观。这里是code:

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

推荐答案

您可以声明一个计算的属性阿贾克斯身上。像这样

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});
}

然后将它添加在铁AJAX

And then adding it on iron-ajax

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