javascript 通过代理服务器发出 jQuery.ajax 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23448886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:53:39  来源:igfitidea点击:

Making jQuery.ajax request through a proxy server

javascriptjqueryajaxproxy-server

提问by Ziink

I'm writing a Chrome extension. If you make jQuery.ajax request for a regular http page from within a page served via https, then the request is blocked by Chrome. I was wondering if I could fetch the requested page using a secure proxy.

我正在编写 Chrome 扩展程序。如果您从通过 https 提供服务的页面内向常规 http 页面发出 jQuery.ajax 请求,则该请求将被 Chrome 阻止。我想知道是否可以使用安全代理获取请求的页面。

So, is it possible to use a generic proxy server for some jQuery.ajax request? If so, how? Note, changing the proxy setting of the browser is not an option.

那么,是否可以对某些 jQuery.ajax 请求使用通用代理服务器?如果是这样,如何?请注意,不能更改浏览器的代理设置。

回答by fjc

Yes, it is.

是的。

What we did at work was implement a proxy that does exactly that:

我们在工作中所做的是实现一个代理,它正是这样做的:

  1. It takes web service calls from the same origin, then,
  2. on the server side, maps them to a webservice of another origin,
  3. sends them there,
  4. receives the results and
  5. passes them on back to the caller.
  1. 它接受来自同一来源的 Web 服务调用,然后,
  2. 在服务器端,将它们映射到另一个来源的网络服务,
  3. 把他们送到那里,
  4. 接收结果并
  5. 将它们传回给调用者。

This way you can both comply with the same origin policy and work with other origins. However, you will always need a server-side proxy functionality.

这样,您既可以遵守相同的来源政策,又可以与其他来源合作。但是,您将始终需要服务器端代理功能。

For an example, take a look at https://developer.yahoo.com/javascript/howto-proxy.html. They offer a PHP implementation for Yahoo web services.

例如,请查看https://developer.yahoo.com/javascript/howto-proxy.html。他们为 Yahoo 网络服务提供了一个 PHP 实现。

回答by Yan Foto

[And a year goes on...] If I understood your question correctly, you want to change your AJAX request depending on the webpage you are currently at. jQuery provides a number of AJAX relatedmethods which might help you with this.

[又一年过去了...] 如果我正确理解了您的问题,您想根据您当前所在的网页更改您的 AJAX 请求。jQuery 提供了许多与AJAX 相关的方法,可以帮助您解决这个问题。

My suggestion is to use jQuery.ajaxPrefilterand adapt your query to use the proxy instead of the original host. An example from the documentation:

我的建议是使用jQuery.ajaxPrefilter并调整您的查询以使用代理而不是原始主机。文档中的一个例子:

$.ajaxPrefilter(function( options ) {
  if ( options.crossDomain ) {
    options.url = "http://example.com/proxy/" + encodeURIComponent( options.url );
    options.crossDomain = false;
  }
});

To spice it up a little bit, you could also use any of the global AJAX event handlersto monitor your request. For example to see if any of the requests fail:

为了增加一点趣味,您还可以使用任何全局 AJAX 事件处理程序来监视您的请求。例如查看是否有任何请求失败:

$( document ).ajaxError(function() {
 console.log("Somethin' went wrawng!");
});