【问题标题】:iron-ajax with basic auth not working具有基本身份验证的 Iron-ajax 不起作用
【发布时间】:2016-10-06 06:07:09
【问题描述】:

我正在尝试使用 Iron-ajax 从我的聚合物前端向我的 java-jersey 后端发出请求,但如果不将其硬编码为属性,似乎无法找到一种方法来进行基本身份验证。我不知道在哪里编写标头制作方法以便它被 Iron-ajax 使用。我尝试将它放在 Polymer({}) 内部和外部,但它仍然没有被调用。这是我的代码。请帮忙。谢谢。

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

    <dom-module id="activity-feed">
        <style is="custom-style" include="iron-flex iron-flex-alignment">
            .cont{ height:90vh; overflow: auto;}
    core-list .row{height: 80px;padding: 16px}
        </style>
        <template>
        <div class="cont">
            <iron-ajax
            auto
            url="http://localhost:8080/tip/webapi/home"
            handle-as="json"
            method="GET"
            headers='{"Authorization": {{makeheaders(gi,tanu)}}}'
            last-response="{{gogo}}"
            ></iron-ajax>
            <div>{{gogo.id}}</div>              
            </div>
        </template>
    </dom-module>
    <script>
        Polymer({
            is : "activity-feed",
            makeheaders: function(user,pass){
                console.log('ceva tot se intampla');
            },
        });
        function makeheaders(user, pass) {
            console.log('merge functia');
            return "Basic " + btoa(user + ":" + pass);
        }
    </script>

【问题讨论】:

    标签: ajax polymer polymer-1.0


    【解决方案1】:

    您的 iron-ajax 元素设置为“auto”,这会在启动时启动 api 请求。您可以删除 auto 属性,并为用户定义一个组合观察者并传递以计算标头,然后手动发送请求:

    <link rel="import" href="../bower_components/polymer/polymer.html">
    <link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
    
    <dom-module id="activity-feed">
        <style is="custom-style" include="iron-flex iron-flex-alignment">
            .cont{ height:90vh; overflow: auto;}
    core-list .row{height: 80px;padding: 16px}
        </style>
        <template>
        <div class="cont" >
            <iron-ajax
            id="xhr"
            url="http://localhost:8080/tip/webapi/home"
            handle-as="json"
            method="GET"
            headers='[[headers]]'
            last-response="{{gogo}}"
            ></iron-ajax>
            <div>{{gogo.id}}</div>
            </div>
        </template>
    </dom-module>
    <script>
        Polymer({
            is : "activity-feed",
    
            properties: {
                headers: {
                    type: Object,
                    value: function() { return {}; }
                },
                user: String,
                pass: String
            },
    
            makeHeaders: function(user,pass){
                return "Basic " + window.btoa(user + ":" + pass);
            },
    
            observers: ['_setBasicAuth(user, pass)'],
    
            _setBasicAuth(user, pass){
                // might be needed
                this.headers['X-Requested-With'] = "XMLHttpRequest";
                // this sets your Auth header
                this.headers['Authorization'] = this.makeHeaders(user, pass);
                this.$.xhr.generateRequest();
            }
    
        });
    
    </script>
    

    【讨论】:

    • 谢谢斯特凡!那么它不起作用的原因是因为'auto'属性?我对 JS 很陌生,所以如果你也能告诉我如何触发请求(手动或其他方式),那就太棒了。
    • 当然 iron-ajax 也可以在自动模式下工作。您只需要摆弄内联生成 base64 字符串。我不确定 iron-ajax 元素的 headers 参数中内联定义的正确语法。我会坚持观察者或没有硬编码凭据的计算绑定
    • 好的,现在它什么也没做。如果可能的话,您能否让我知道我需要做什么才能触发请求?我想知道该命令适用于上述凭据代码:用户:hau,传递:les。如果你能把它放在你已经给我的例子中,那就太好了。谢谢十亿!
    • 请求在 _setBasicAuth() 中使用 this.$.xhr.generateRequest() 触发。当属性 user 和 pass 都有值时执行观察者。为了让它运行,在你的 HTML 中插入你的自定义元素,给它一个 id 属性并设置用户/密码属性:var a = document.getElementById('idOfYourElement'); a.user='hau'; a.pass='les';
    • 这些命令去哪儿了?它是在 Polymer({here?}) 内部还是外部?你能把它写在上面的代码上吗? (你的代码,不是我的。)谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 2014-09-06
    • 2014-01-22
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    相关资源
    最近更新 更多