聚合物铁阿贾克斯:如何将输入元素中的数据绑定到铁阿贾克斯的主体属性 [英] polymer iron-ajax : How to Bind data from input element to iron-ajax's body attribute

查看:29
本文介绍了聚合物铁阿贾克斯:如何将输入元素中的数据绑定到铁阿贾克斯的主体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在将数据从输入元素绑定到 Iron-ajax 的body"属性时遇到问题.当我在聚合物 0.5 上使用 core-ajax 时,我可以轻松地绑定这样的值:

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>

现在我用铁阿贾克斯尝试了同样的事情.但它按字面意思发送{{username}}"和{{password}}"而不是它们的值.代码如下:

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

推荐答案

您可以为 ajax 主体声明一个计算属性.像这样

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

然后将其添加到 Iron-ajax 上

And then adding it on iron-ajax

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

这篇关于聚合物铁阿贾克斯:如何将输入元素中的数据绑定到铁阿贾克斯的主体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆