Basic Authentication with Polymer-ajax Core Elements

Has anyone tried to extract data from a URL with basic authentication using the core-ajax element in Polymer?

this is the standard element I use:

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

      

and I found here ( http://un.codiert.org/2014/09/polymer-core-ajax-basic-authorization/ ) that I need to add this JS

this. $. ajax.headers = '{"X-Requested-With": "XMLHttpRequest" +', "Authorization": "Basic" + btoa (this.username + ":" + this.password) + '"}';

I'm not sure where exactly I should add the JS ...

+3


source to share


3 answers


It will look like this ...



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

Run codeHide result


+1


source


Have you tried the headers attribute for ajax-core? Like this:



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

      

0


source


Found it worked for me. Just found a js variable call.

<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) + '"}',

      

0


source







All Articles