2015-11-23 90 views
7

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

直到我们限制访问我们的代理服务器以限制从我们的C5站点(这也是我们想要的)的SSL时,它才有效。

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

本质上,我需要做的(我认为)是在C5内部移动GET请求,以便它可以通过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() { 
    .... 
} 
+0

难道您不能在服务器上反弹请求,实质上是使用它作为代理吗? –

+1

欢迎来到SO @sethmrtn。为了满足SO的要求(我们不提问问题,我们试图让它们重用),我已经编辑了你的问题。 –

+0

什么是C5网站,为什么不能向它发送GET请求?它没有实施CORS?什么是路障? –

回答

0

你可以做一个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