• <i id='DLtOR'><tr id='DLtOR'><dt id='DLtOR'><q id='DLtOR'><span id='DLtOR'><b id='DLtOR'><form id='DLtOR'><ins id='DLtOR'></ins><ul id='DLtOR'></ul><sub id='DLtOR'></sub></form><legend id='DLtOR'></legend><bdo id='DLtOR'><pre id='DLtOR'><center id='DLtOR'></center></pre></bdo></b><th id='DLtOR'></th></span></q></dt></tr></i><div id='DLtOR'><tfoot id='DLtOR'></tfoot><dl id='DLtOR'><fieldset id='DLtOR'></fieldset></dl></div>

    <tfoot id='DLtOR'></tfoot>
  • <small id='DLtOR'></small><noframes id='DLtOR'>

  • <legend id='DLtOR'><style id='DLtOR'><dir id='DLtOR'><q id='DLtOR'></q></dir></style></legend>

        <bdo id='DLtOR'></bdo><ul id='DLtOR'></ul>

        如何在来自 javascript 的 REST API 调用中进行 http 身

        时间:2023-10-13
        <i id='tHfYf'><tr id='tHfYf'><dt id='tHfYf'><q id='tHfYf'><span id='tHfYf'><b id='tHfYf'><form id='tHfYf'><ins id='tHfYf'></ins><ul id='tHfYf'></ul><sub id='tHfYf'></sub></form><legend id='tHfYf'></legend><bdo id='tHfYf'><pre id='tHfYf'><center id='tHfYf'></center></pre></bdo></b><th id='tHfYf'></th></span></q></dt></tr></i><div id='tHfYf'><tfoot id='tHfYf'></tfoot><dl id='tHfYf'><fieldset id='tHfYf'></fieldset></dl></div>
          <bdo id='tHfYf'></bdo><ul id='tHfYf'></ul>
        • <small id='tHfYf'></small><noframes id='tHfYf'>

                <tbody id='tHfYf'></tbody>

                <tfoot id='tHfYf'></tfoot>
                  <legend id='tHfYf'><style id='tHfYf'><dir id='tHfYf'><q id='tHfYf'></q></dir></style></legend>
                  本文介绍了如何在来自 javascript 的 REST API 调用中进行 http 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要从 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模板网!

                  上一篇:在 BlobBuilder 中更改文件名以作为 XHR 上的 FormDa 下一篇:执行用包含脚本标签的 XMLHttpRequest 编写的 Javas

                  相关文章

                  最新文章

                  • <bdo id='hhnUj'></bdo><ul id='hhnUj'></ul>
                • <i id='hhnUj'><tr id='hhnUj'><dt id='hhnUj'><q id='hhnUj'><span id='hhnUj'><b id='hhnUj'><form id='hhnUj'><ins id='hhnUj'></ins><ul id='hhnUj'></ul><sub id='hhnUj'></sub></form><legend id='hhnUj'></legend><bdo id='hhnUj'><pre id='hhnUj'><center id='hhnUj'></center></pre></bdo></b><th id='hhnUj'></th></span></q></dt></tr></i><div id='hhnUj'><tfoot id='hhnUj'></tfoot><dl id='hhnUj'><fieldset id='hhnUj'></fieldset></dl></div>

                  1. <small id='hhnUj'></small><noframes id='hhnUj'>

                      <legend id='hhnUj'><style id='hhnUj'><dir id='hhnUj'><q id='hhnUj'></q></dir></style></legend>
                    1. <tfoot id='hhnUj'></tfoot>