使用core-ajax Polymer元素进行基本身份验证

时间:2014-11-21 10:34:36

标签: polymer basic-authentication http-basic-authentication

是否有人尝试使用基本身份验证从URL检索数据,使用Polymer中的core-ajax元素?

这是我使用的标准元素:

<core-ajax id="ajax"
                   auto
                   url="hereIputmyURL"
                   handleAs="json"
                   method="GET"
                   on-core-response="{{postsLoaded}}"
                   >
    </core-ajax>

我发现这里(http://un.codiert.org/2014/09/polymer-core-ajax-basic-authorization/)我需要添加这个JS

  

这个。$。ajax.headers =&#39; {&#34; X-Requested-With&#34;:&#34; XMLHttpRequest&#34;&#39;                 +&#39;,&#34;授权&#34;:&#34;基本&#39;                 + btoa(this.username +&#34;:&#34; + this.password)+&#39;&#34;}&#39;;

我不确定我在哪里添加JS ...

3 个答案:

答案 0 :(得分:1)

看起来像这样......

&#13;
&#13;
<template>
  <core-ajax id="ajax" auto url="hereIputmyURL" 
             handleAs="json" method="GET" 
             on-core-response="{{postsLoaded}}">
  </core-ajax>
  <div>
    <content></content>
  </div>
</template>
<script>
  Polymer({
    ready: function(){
       this.$.ajax.headers = '{"X-Requested-With": "XMLHttpRequest"' + 
                          ', "Authorization": "Basic ' + btoa(this.username 
                          + ":" + this.password) + '"}';
       });
    }
</script>
</polymer-element>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你试过&#34;标题&#34; ajax-core的属性? 像这样:

    <core-ajax id="ajax"
                   auto
                   url="hereIputmyURL"
                   handleAs="json"
                   method="GET"
                   on-core-response="{{postsLoaded}}"
                   headers = '{"X-Requested-With": "XMLHttpRequest"}'
                   >
    </core-ajax>

答案 2 :(得分:0)

发现这对我有用。刚发现调用js变量有效。

<core-ajax
    url="http://some.url"
    handleAs="json"
    method="post"
    on-core-response="{{getDetails}}"
    headers="{{headers}}"></core-ajax>



Polymer('element_name', {
    username: 'usernmae',
    password: 'password',
    headers: '{"X-Requested-With": "XMLHttpRequest"' + ', "Authorization": "Basic ' + btoa(this.username + ":" + this.password) + '"}',