最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - polymer iron-ajax : How to Bind data from input element to iron-ajax's body attribute - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

You 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>
发布评论

评论列表(0)

  1. 暂无评论