通过HTTPS页面的HTTP Ajax请求

我有一个网站与HTTPS连接的一些页面。 从这些HTTPS页面,我必须使用HTTP Ajax请求来检索像空白字段这样的错误。 但是这个错误信息不会来。 有没有解决scheme,或者我必须使这个AJAX请求文件HTTPS连接?

由于同源策略,这是不可能的。

您将需要将Ajax请求切换到https。

在某些情况下,没有响应的单向请求可以触发到TCP服务器,而不需要SSL证书。 与HTTP服务器相比,TCP服务器会接收到您的请求。 但是,浏览器将不会访问任何数据,因为如果没有正确的证书检查,浏览器将不会发送任何数据。 在特殊情况下,即使是没有任何数据的裸露TCP信号也足以执行一些任务。 例如,LAN中的物联网设备启动到外部服务的连接。 链接

这是一种“唤醒”触发器,在没有任何安全性的情况下在端口上工作。

如果需要响应,可以使用安全的公共https服务器来实现,该服务器可以使用例如Websockets将所需的数据发送回浏览器。

不过,这可以通过以下步骤完成:

  1. 发送一个https ajax请求到您的网站(相同的域名)

    jQuery.ajax({ 'url' : '//same_domain.com/ajax_receiver.php', 'type' : 'get', 'data' : {'foo' : 'bar'}, 'success' : function(response) { console.log('Successful request'); } }).fail(function(xhr, err) { console.error('Request error'); }); 
  2. 得到ajax请求,例如通过php,并通过http做一个CURL获取请求到任何想要的网站。

     use linslin\yii2\curl; $curl = new curl\Curl(); $curl->get('http://example.com'); 

没有任何服务器端解决scheme,Theres只是一个安全的页面可以从一个不安全的页面/请求中得到的东西的一种方式,这被认为是postMessage和一个popup

我说popupcuz该网站是不允许​​混合的内容。 但是一个popup窗口不是真的混合。 它有它自己的窗口,但仍然能够通过postMessage与开瓶器沟通。

所以你可以用window.open(...)打开一个新的http页面,并向你提出请求(也就是说,如果该站点也在使用CORS)


当我写这篇文章的时候, XDomain就浮现在脑海中,但是这里使用新的fetch api是一种现代化的方法,优点是大文件的stream式处理,缺点是它不能在所有的浏览器中运行

你把这个代理脚本放在任何http页面上

 onmessage = evt => { const port = evt.ports[0] fetch(...evt.data).then(res => { // the response is not clonable // so we make a new plain object const obj = { bodyUsed: false, headers: [...res.headers], ok: res.ok, redirected: res.redurected, status: res.status, statusText: res.statusText, type: res.type, url: res.url } port.postMessage(obj) // Pipe the request to the port (MessageChannel) const reader = res.body.getReader() const pump = () => reader.read() .then(({value, done}) => done ? port.postMessage(done) : (port.postMessage(value), pump()) ) // start the pipe pump() }) } 

然后,在https页面中打开一个popup窗口(请注意,只能在用户交互事件中执行此操作,否则将会被阻止)

 window.popup = window.open(http://.../proxy.html) 

创build您的实用function

 function xfetch(...args) { // tell the proxy to make the request const ms = new MessageChannel popup.postMessage(args, '*', [ms.port1]) // Resolves when the headers comes return new Promise((rs, rj) => { // First message will resolve the Response Object ms.port2.onmessage = ({data}) => { const stream = new ReadableStream({ start(controller) { // Change the onmessage to pipe the remaning request ms.port2.onmessage = evt => { if (evt.data === true) // Done? controller.close() else // enqueue the buffer to the stream controller.enqueue(evt.data) } } }) // Construct a new response with the // response headers and a stream rs(new Response(stream, data)) } }) } 

并像通常一样使用提取api来完成请求

 xfetch('http://httpbin.org/get') .then(res => res.text()) .then(console.log)