[英]Authentication headers in REST API call
在我的Java Web應用程序中,我添加了一個Web服務調用@ URL 10.10.10.21:8088/openbravo/com.data.service.
當我從瀏覽器中訪問URL時,它會要求我輸入用戶名和密碼。 輸入這些詳細信息后,我可以看到json輸出(所需的輸出)。
Web服務提供了JSON REST服務。 我想實現一個基本的客戶端UI來獲取此JSON數據並發布在頁面上。 作為客戶端的一部分,我已經用jQuery ajax編寫了基本的html代碼來調用此URL。 但是我得到401未經授權的回應。
$("input#add-contact").on("click", function () {
$.ajax({
type: "POST",
url: "http://10.10.10.21:8088/openbravo/com.indavest.openbravo.restaurantpos.service",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
console.log(response);
}
});
});
如何將身份驗證標頭添加到呼叫中。 以上代碼行是調用其余Web服務的正確方法嗎? 我是Webservices概念的新手。 任何幫助都很好!!! 謝謝。
用於基本身份驗證:
$.ajax({
type: "post",
url: "http://localhost:8080/myservice/function?format=json",
headers: { "Authorization" : makeBasicAuth("1", "1") },
data: jsonText,
contentType: "application/json; charset=UTF-8",
dataType: "json",
success: function( data, statusTxt, xjobj )
{
showSuccess( data, statusTxt, xjobj, "blabla" );
},
error: function( xjobj, statusTxt, errorThrown )
{
showError( statusTxt, xjobj, errorThrown, "blabla" );
}
});
有:
function makeBasicAuth(user, password)
{
var tok = user + ':' + password;
var hash = btoa(tok);
return "Basic " + hash;
}
但這是基本的,也有摘要式身份驗證(我建議您在Wikipedia中對這些http技術進行很好的解釋),所以,兒子的想法很相似。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.