問題描述
我需要從 Java 腳本調用 OpenMRS REST API 以從 OpenMRS 獲取數據.下面是我的java腳本代碼:
I need to call OpenMRS REST API from Java script to get data from OpenMRS. Below is my java script code:
function myfunction(){
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:8081/openmrs-standalone/ws/rest/v1/person?q=John", false);
xhr.setRequestHeader("Authorization: Basic YWRtaW46QWRtaW4xMjM");
xhr.send("");
alert(xhr.status);
}
YWRtaW46QWRtaW4xMjM
是我的 base64 編碼的用戶名:密碼,如 這里.如果我沒有將授權行放入代碼中并使用 Firebug 檢查 Web 應用程序,它會返回預期的 401 未授權狀態.但是,如果我授權,則不會返回任何內容,并且在螢火蟲中我也看不到任何響應.如果我直接在瀏覽器上檢查 URL,頁面會詢問用戶名和密碼,并在提供正確的憑據后,它會正常返回數據.因此,我遇到了一些從應用程序的 java 腳本提供 http 身份驗證的問題.我還考慮了這里解釋的方法,但沒有運氣.誰能幫我從 javascript 授權 http 請求?
Where YWRtaW46QWRtaW4xMjM
is my base64 coded username:password as explained here. If I do not put the authorization line in the code and check the web app using Firebug, it returns 401 unauthorized status that is expected. But if I put the authorization, nothing is returned and in firebug I do not see any response as well. If I check the URL directly on browser, the page asks for username and password and after giving correct credential, it returns the data normaly. So I am getting some problem of providing the http authentication right from the java script of the app. I have also considered the methods explained here but no luck. Can anyone please help me to authorize the http request right from the javascript?
推薦答案
這是另一個類似但不同的示例,說明如何為授權目的設置標頭,但使用 JQuery 和 AJAX.
Here is another similar but different example of how to set the header for authorization purposes, but instead using JQuery and AJAX.
var token = "xyz"
var url = "http://localhost:8081/openmrs-standalone/ws/rest/v1/person?q=John"
$.ajax({
url: url,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + token)
},
})
.done(function (data) {
$.each(data, function (key, value) {
// Do Something
})
})
.fail(function (jqXHR, textStatus) {
alert("Error: " + textStatus);
})
下面也是一個示例,說明如何使用 xhr 而不是 AJAX 獲取訪問令牌.
Below is also an example of how you might get an access token using xhr instead of AJAX.
var data = "grant_type=password&username=myusername@website.com&password=MyPassword";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://somewebsite.net/token");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("client_id", "4444-4444-44de-4444");
xhr.send(data);
注意跨站點域請求(如果您請求的令牌不在 localhost 或您當前工作的域中),因為您需要 CORS.如果您確實遇到了跨域問題,請參閱本教程以獲取幫助,并且確保您也啟用了來自 API 的 CORS 請求.
Beware of cross-site domain requests(if you're requesting a token that's not on localhost or within the domain that you are currently working in), as you'll need CORS for that. If you do run into a cross-domain issue, see this tutorial for help, and be sure you have enabled CORS requests from the API as well.
這篇關于如何在來自 javascript 的 REST API 調用中進行 http 身份驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!