2015-08-25 97 views
2

我想在Jquery中使用基本身份驗證協議來驗證用戶,但它一直給出錯誤:「XMLHttpRequest無法加載https://api.credly.com/v1.1/authenticate。請求標頭字段授權不被Access-控制允許頭「。基本身份驗證不工作在jquery,但在其他語言工作

但是,當我使用郵遞員客戶端檢查API時,它工作正常。

jQuery代碼:

var settings = { 
    "async": true, 
    "crossDomain": true, 
    "url": "https://api.credly.com/v1.1/authenticate", 
    "method": "POST", 
    "headers": { 
    "x-api-key": "myAPIkey", 
    "x-api-secret": "mySECRET", 
    "authorization": "Basic "+btoa(<email> + ":" + <password>) 
    } 
} 

$.ajax(settings).done(function (response) { 
    console.log(response); 
}); 

的API正在爲郵差客戶,所以應該在沒有任何問題,但它不是在jQuery的工作。 請幫忙。

+2

可能重複的[ '是不允許的請求頭字段授權' 的錯誤 - Tastypie](http://stackoverflow.com/questions/10548883/request- header-field-authorization-is-not-allowed-error-tastypie) – D4V1D

回答

0

使用dataType : 'jsonp'交叉DOMIAN請求,See also

var settings = { 
    "async": true, 
    "crossDomain": true, 
    dataType : 'jsonp', //Cross Domain request 
    "url": "https://api.credly.com/v1.1/authenticate", 
    "method": "POST", 
    "headers": { 
    "x-api-key": "myAPIkey", 
    "x-api-secret": "mySECRET", 
    "authorization": "Basic "+btoa(<email> + ":" + <password>) 
    } 
} 
+0

我試過jsonp,但由於某種原因,我的POST請求在Chrome控制檯中顯示爲GET – siddhant

+0

@siddhant是什麼?其他瀏覽器怎麼樣/ –

+0

我剛剛發現,jsonp無法發出POST請求請參閱此鏈接:http://stackoverflow.com/questions/3860111/how-to-make-a-jsonp-post-request-that -specification-contenttype-with-jquery – siddhant

相關問題