安全Javascript GET请求

Secure Javascript GET Request

本文关键字:请求 GET Javascript 安全      更新时间:2023-09-26

本质上,我有一个自定义的HTML图表,它需要来自外部安全代理服务器的值。现在,我正在将HTML块插入页面上包含JavaScript的相关区域,以便通过XHTTP get请求获得正确的数据。

它非常有效,直到我们将对代理服务器的访问限制在C5站点的SSL(这也是我们想要的)。

这会阻止图表获得正确的值,因为HTML和JavaScript是在客户端执行的,而不是通过C5执行的。

从本质上讲,我需要做的(我认为)是将GET请求移动到C5内部,以便它可以通过SSL证书。然后我需要取这个值,然后把它插回图表中。

下面是一些基于HTML代码的伪代码,我目前正在将其放入页面中。

<!-- This is the actual HTML block that I'm creating. -->
<div id="HTMLBlock455" class="HTMLBlock">
<div class="someCustomChart">
<!-- Here's the script currently running that gets the necessary data and calls the proper functions to populate the chart.  -->
<script type="text/javascript">
// Global Var to store updating value 
var amount = 0;
// Open a new HTTP Request)
var xhr = new XMLHttpRequest();
xhr.open("GET", "Some_ElasticSearch Server", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4) {
    if (xhr.status === 200) {
      var person = JSON.parse(xhr.responseText);
      amount = person._source.age; // Grabs the person age
      $('#chart_328').data('updateto', amount); //updates the above HTML data    value
            document.getElementById('chart_328_text').innerHTML = amount + '%';
    } else {
      console.error(xhr.statusText);
   }
  }
};
xhr.onerror = function (e) {
 console.error(xhr.statusText);
};
xhr.send(null);
// This function executes on page load and prepares the chart!
$(document).ready(function() {
   ....
}

您可以对另一个域或协议执行Ajax请求,只需在您想要访问的后端启用CORS

另一个选项是创建一个代理通行证请求,但我不知道这在C5中是否可用。在这种情况下,C5将代理您的请求。那么流程是:

Ajax request to your C5 -> C5 proxies the request to the external resource -> C5 sends you back the result

我更喜欢CORS的方法,但考虑到传统浏览器不可能100%兼容。请参阅参考资料:http://caniuse.com/#feat=cors