2016-07-26 31 views
1

我正在做一個交叉原點請求,以從具有基本身份驗證的url中獲取json響應。所以點擊按鈕,設置基本認證的標題。當我在發送請求之前打印出值時,我會看到它們。但是,當我檢查鉻開發工具的請求,我沒有看到頭被設置。此外,該請求變成來自「GET」請求的「選項」請求,並且響應爲未經授權的401。有什麼遺漏嗎?Polymer.js iron-ajax頭基本授權不起作用

<paper-input type="search" value="{{imageUrl}}" placeholder="Enter Image URL"></paper-input> 
    <div class="buttons"> 
    <paper-button on-tap="getData" autofocus>Search</paper-button> 
    </div> 
    <iron-ajax 
    id="xhr" 
    url="https://api.com/v1" 
    params='{"url":"some query value"}' 
    handle-as="json" 
    method="GET" 
    last-response="{{ajaxResponse}}"></iron-ajax> 

    getData: function(){ 
    this.$.xhr.headers['X-Requested-With'] = "XMLHttpRequest"; 
    this.$.xhr.headers['Authorization'] = this.makeHeaders(this.user, this.password); 
    this.$.xhr.generateRequest(); 
    } 
+0

這讓我相信兄弟wser正在使用options方法向「https://api.com/vi」發送預檢請求,並且您需要通過發回http狀態200來處理該選項方法,然後您應該正常處理該呼叫。 – getbuckts

+0

檢查這些網址:http://stackoverflow.com/questions/30632200/standalone-spring-oauth2-jwt-authorization-server-cors/30638914#30638914 http://stackoverflow.com/questions/8685678/cors-how- do-preflight-an-httprequest – getbuckts

+0

@getbuckts它實際上是我打的第三方api服務,它在我從郵遞員那裏工作時起作用。此外,我嘗試與safari和firefox相同的失敗消息。不知道我在做錯什麼 –

回答

1

默認情況下iron-ajax默認情況下,跨站點不發送credentials請求

您可以通過設置在iron-ajaxwithCredentials屬性更改此:

<iron-ajax 
    id="xhr" 
    url="https://api.com/v1" 
    params='{"url":"some query value"}' 
    handle-as="json" 
    with-credentials="true" 
    method="GET" 
    last-response="{{ajaxResponse}}"></iron-ajax> 

this.$.xhr.withCredentials = true;