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

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

programmeradmin0浏览0评论

I have a polymer-element and I want to apply a token as headers attribute.

When I push the button an XMLHttpReqeust is send. The responsible iron-ajax element has a headers property with a string. I would like to change the string, and apply a different attribute.

I've been told that normal pound bindings does not work and I should try puted bindings or just puted properties.

But the problem seems to be the question, how to bind these puted properties or puted bindings to the iron-ajax element?

Whenever I use curly braces, nothing gets evaluated. And if I leave them out, only the remaining string gets parsed.

Here is my element:

<link rel="import" href="../bower_ponents/polymer/polymer.html">
<link rel="import" href="../bower_ponents/iron-ajax/iron-ajax.html">

<dom-module id="demo-element">
    <template>
        <button on-click="sendXMLHttpRequest">sendXMLHttpRequest</button>

        <div>
            Computed Binding HeadersProperty: 
            <span>{{puteHeadersProperty(csrfToken)}}</span>
        </div>
        <div>
            Computed Property HeadersProperty: 
            <span>{{headersProperty}}</span>
        </div>
        <div>
            Computed Binding HeadersToken: 
            <span>{{puteHeadersToken(csrfToken)}}</span>
        </div>
        <div>
            Computed Property HeadersToken: 
            <span>{{headersToken}}</span>
        </div>

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

    </template>
    <script>
        Polymer({
            is: 'demo-element',

            properties: {
                csrfToken: {
                    type: String,
                    value: 'aBcDeF'
                },
                headersProperty: {
                    type: String,
                    puted: 'puteHeadersProperty(csrfToken)'
                },
                headersToken: {
                    type: String,
                    puted: 'puteHeadersToken(csrfToken)'
                }

            },

            sendXMLHttpRequest: function () {
                // ajax call
                this.$.ajax.body = this.headersProperty;
                this.$.ajax.generateRequest();
            },

            puteHeadersProperty: function (csrfToken) {
                return '{"X-CSRF-Token":\"' + csrfToken + '\"}';
            },

            puteHeadersToken: function (csrfToken) {
                return csrfToken;
            }
        });
    </script>
</dom-module>

As you can see I have created puted bindings and properties for the plete headers property and also just for the token I want to pass in.

But the iron-ajax element does not evaluate anything, when I use curly braces.

Here is what I have tried in the iron-ajax element so far:

  1. headers='{"X-CSRF-Token": "{{csrfToken}}"}' // doesn't evaluate
  2. headers='{"X-CSRF-Token": "{{headersToken}}"}' // doesn't evaluate
  3. headers="{{headersProperty}}" // doesn't evaluate
  4. headers="{{puteHeadersProperty(csrfToken)}}" // doesn't evaluate
  5. headers='{"X-CSRF-Token": "{{puteHeadersToken(csrfToken)}}"}' // doesn't evaluate
  6. headers='{"X-CSRF-Token": "headersToken"}' // evalutes to {"X-CSRF-Token": "headersToken"}
  7. headers='puteHeadersProperty(csrfToken)' // doesn't evaluate

So, how do I have to bind an attribute to the headers property of the iron-ajax element?


EDIT


Some users have have provided a "working solution", which is unfortunately not working, because the headers seem to be set, but it is not included in the headers request. Proof:

Compare this to the case when we use a Random String:

Here the x-csrf-token is properly set.

Because it is not possible to check the request in the provided JSBIN I included a git repository here with the example code:

I had to include dependencies instead of the polygit resources, because polygit throws errors when working with iron-ajax.


How to reproduce the error?

  1. git clone .git
  2. Open DevTools and Hit the Request Button. You will see that no headers are set. Change in the iron-ajax element the headers property on line 50 from

headers='{{puteHeadersProperty(csrfToken)}}'

to

headers='{"X-CSRF-Token":"ARandomString"}'

  1. Hit the Request Button again and check in Dev-Tools to see that the token is now accurately set in the headers request.

What I think the error boils down to

Judging by the Output of the Console, the errors seems to be, that the headings property in the iron-ajax element expects an Object, but the evaluation from the databinding returns a string.

I hope somebody can at least reproduce the error.

I have a polymer-element and I want to apply a token as headers attribute.

When I push the button an XMLHttpReqeust is send. The responsible iron-ajax element has a headers property with a string. I would like to change the string, and apply a different attribute.

I've been told that normal pound bindings does not work and I should try puted bindings or just puted properties.

But the problem seems to be the question, how to bind these puted properties or puted bindings to the iron-ajax element?

Whenever I use curly braces, nothing gets evaluated. And if I leave them out, only the remaining string gets parsed.

Here is my element:

<link rel="import" href="../bower_ponents/polymer/polymer.html">
<link rel="import" href="../bower_ponents/iron-ajax/iron-ajax.html">

<dom-module id="demo-element">
    <template>
        <button on-click="sendXMLHttpRequest">sendXMLHttpRequest</button>

        <div>
            Computed Binding HeadersProperty: 
            <span>{{puteHeadersProperty(csrfToken)}}</span>
        </div>
        <div>
            Computed Property HeadersProperty: 
            <span>{{headersProperty}}</span>
        </div>
        <div>
            Computed Binding HeadersToken: 
            <span>{{puteHeadersToken(csrfToken)}}</span>
        </div>
        <div>
            Computed Property HeadersToken: 
            <span>{{headersToken}}</span>
        </div>

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

    </template>
    <script>
        Polymer({
            is: 'demo-element',

            properties: {
                csrfToken: {
                    type: String,
                    value: 'aBcDeF'
                },
                headersProperty: {
                    type: String,
                    puted: 'puteHeadersProperty(csrfToken)'
                },
                headersToken: {
                    type: String,
                    puted: 'puteHeadersToken(csrfToken)'
                }

            },

            sendXMLHttpRequest: function () {
                // ajax call
                this.$.ajax.body = this.headersProperty;
                this.$.ajax.generateRequest();
            },

            puteHeadersProperty: function (csrfToken) {
                return '{"X-CSRF-Token":\"' + csrfToken + '\"}';
            },

            puteHeadersToken: function (csrfToken) {
                return csrfToken;
            }
        });
    </script>
</dom-module>

As you can see I have created puted bindings and properties for the plete headers property and also just for the token I want to pass in.

But the iron-ajax element does not evaluate anything, when I use curly braces.

Here is what I have tried in the iron-ajax element so far:

  1. headers='{"X-CSRF-Token": "{{csrfToken}}"}' // doesn't evaluate
  2. headers='{"X-CSRF-Token": "{{headersToken}}"}' // doesn't evaluate
  3. headers="{{headersProperty}}" // doesn't evaluate
  4. headers="{{puteHeadersProperty(csrfToken)}}" // doesn't evaluate
  5. headers='{"X-CSRF-Token": "{{puteHeadersToken(csrfToken)}}"}' // doesn't evaluate
  6. headers='{"X-CSRF-Token": "headersToken"}' // evalutes to {"X-CSRF-Token": "headersToken"}
  7. headers='puteHeadersProperty(csrfToken)' // doesn't evaluate

So, how do I have to bind an attribute to the headers property of the iron-ajax element?


EDIT


Some users have have provided a "working solution", which is unfortunately not working, because the headers seem to be set, but it is not included in the headers request. Proof:

Compare this to the case when we use a Random String:

Here the x-csrf-token is properly set.

Because it is not possible to check the request in the provided JSBIN I included a git repository here with the example code: https://github./LoveAndHappiness/polymer-iron-ajax-issue-159

I had to include dependencies instead of the polygit resources, because polygit throws errors when working with iron-ajax.


How to reproduce the error?

  1. git clone https://github./LoveAndHappiness/polymer-iron-ajax-issue-159.git
  2. Open DevTools and Hit the Request Button. You will see that no headers are set. Change in the iron-ajax element the headers property on line 50 from

headers='{{puteHeadersProperty(csrfToken)}}'

to

headers='{"X-CSRF-Token":"ARandomString"}'

  1. Hit the Request Button again and check in Dev-Tools to see that the token is now accurately set in the headers request.

What I think the error boils down to

Judging by the Output of the Console, the errors seems to be, that the headings property in the iron-ajax element expects an Object, but the evaluation from the databinding returns a string.

I hope somebody can at least reproduce the error.

Share Improve this question edited Dec 3, 2015 at 12:01 LoveAndHappiness asked Dec 2, 2015 at 12:10 LoveAndHappinessLoveAndHappiness 10.1k22 gold badges74 silver badges107 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

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

Working example: http://jsbin./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 puted 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/polymer+:master/ponents/">
    <!-- Third-party imports -->
    <script src="webponentsjs/webponents-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="{{puteHeadersProperty(csrfToken)}}"
               >
          </iron-ajax>
      </template>
    </dom-module>
    <script>
        Polymer({
            is: 'demo-element',

            properties: {
                csrfToken: {
                    type: String,
                    value: 'aBcDeF'
                },
                headersProperty: {
                    type: String,
                    puted: 'puteHeadersProperty(csrfToken)'
                },
                headersToken: {
                    type: String,
                    puted: 'puteHeadersToken(csrfToken)'
                }
            },

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

            puteHeadersProperty: function (csrfToken) {
                return {"X-CSRF-Token": ' + csrfToken };
            }
        });
    </script>  
  </head>
  <body unresolved class="fullbleed layout vertical">
    <demo-element></demo-element>  
  </body>
</html>
发布评论

评论列表(0)

  1. 暂无评论