使用核心 ajax 聚合物元素进行基本身份验证



有没有人尝试使用Polymer中的核心ajax元素使用基本身份验证从URL检索数据?

这是我使用的标准元素:

<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

this.$.ajax.headers='{"X-Request-With": "XMLHttpRequest"' + ', "授权": "基本" + btoa(this.username + ":" + this.password) + '"}';

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

它看起来像这样...

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

您是否尝试过 ajax-core 的"headers"属性?喜欢这个:

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

发现这对我有用。刚刚发现调用 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) + '"}',

最新更新