PolymerJS: Iron-Ajax - How to Bind Token to Headers Property?

爱⌒轻易说出口 提交于 2019-12-06 02:47:44

The headersProperty wants to be an Object and not a String.

Working example: http://jsbin.com/rasoqexese/edit?html,output

Many thanks to Peter Burns

The last action on this topic is about half a year ago. I tried the same thing: Adding a token to the header of the iron-ajax request. I tried everything the original poster tried and I tried everything shown in the answers. I searched the net for about three weeks, sometimes several hours each evening. I didn't find a solution. Nothing worked. The properties doesn't evaluate and/or they are not added to the header of the request.

My answer: It looks like it is not possible to add computed properties to the headers of an iron-ajax request yet. Keep in mind that Polymer is a very new framework and a lot of things need to be improved over the next releases.

You are probably looking for attribute binding. This will work for you:

    <iron-ajax
         id="ajax"
         method="POST"
         url=""
         handle-as="json"
         headers$='{"X-CSRF-Token": "{{csrfToken}}"}'
         ></iron-ajax>

I think you need to put notify:true on your csrfToken property.

Here is at least a working JSBIN version:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Polymer Bin</title>
    <base href="http://polygit.org/polymer+:master/components/">
    <!-- Third-party imports -->
    <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
    <link href="iron-ajax/iron-ajax.html" rel="import">
    <!-- Styling -->
    <style>
    </style>
    <dom-module id="demo-element">
      <template>
          <button on-click="sendXMLHttpRequest">sendXMLHttpRequest</button>
          <iron-ajax
               id="ajax"
               method="POST"
               url=""
               handle-as="json"
               headers="{{computeHeadersProperty(csrfToken)}}"
               >
          </iron-ajax>
      </template>
    </dom-module>
    <script>
        Polymer({
            is: 'demo-element',

            properties: {
                csrfToken: {
                    type: String,
                    value: 'aBcDeF'
                },
                headersProperty: {
                    type: String,
                    computed: 'computeHeadersProperty(csrfToken)'
                },
                headersToken: {
                    type: String,
                    computed: 'computeHeadersToken(csrfToken)'
                }
            },

            sendXMLHttpRequest: function () {
                // ajax call
                console.log("Headers in AJAX: " + this.$.ajax.headers);
                //this.$.ajax.body = this.headersProperty;
                //this.$.ajax.generateRequest();
            },

            computeHeadersProperty: function (csrfToken) {
                return {"X-CSRF-Token": ' + csrfToken };
            }
        });
    </script>  
  </head>
  <body unresolved class="fullbleed layout vertical">
    <demo-element></demo-element>  
  </body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!