简体   繁体   English

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

[英]Making jQuery.ajax request through a proxy server

I'm writing a Chrome extension.我正在写一个 Chrome 扩展。 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.如果您从通过 https 提供的页面中向常规 http 页面发出 jQuery.ajax 请求,则该请求会被 Chrome 阻止。 I was wondering if I could fetch the requested page using a secure proxy.我想知道我是否可以使用安全代理获取请求的页面。

So, is it possible to use a generic proxy server for some jQuery.ajax request?那么,是否可以为某些 jQuery.ajax 请求使用通用代理服务器? If so, how?如果是这样,如何? Note, changing the proxy setting of the browser is not an option.请注意,更改浏览器的代理设置不是一个选项。

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,它接受来自同一来源的 Web 服务调用,然后,
  2. on the server side, maps them to a web service of another origin,在服务器端,将它们映射到另一个来源的 Web 服务,
  3. sends them there,把他们送到那里,
  4. receives the results and接收结果并
  5. passes them on back to the caller.将它们传递回调用者。

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.但是,您将始终需要服务器端代理功能。

[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. [又一年过去了...] 如果我正确理解了您的问题,您想根据您当前所在的网页更改您的 AJAX 请求。 jQuery provides a number of AJAX related methods which might help you with this. jQuery 提供了许多与AJAX 相关的方法,可以帮助您解决这个问题。

My suggestion is to use jQuery.ajaxPrefilter and adapt your query to use the proxy instead of the original host.我的建议是使用jQuery.ajaxPrefilter并调整您的查询以使用代理而不是原始主机。 An example from the documentation:文档中的一个示例:

$.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 handlers to monitor your request.为了增加一点趣味,您还可以使用任何全局 AJAX 事件处理程序来监视您的请求。 For example to see if any of the requests fail:例如,查看是否有任何请求失败:

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

You would need an external library to perform Ajax requests via a HTTP Proxy using JQuery. Out-of-the-box, JQuery does not have this functionality.您需要一个外部库来使用 JQuery 通过 HTTP 代理执行 Ajax 请求。开箱即用的 JQuery 没有此功能。 An example of such is https://www.AjaxProxy.com which can be used with your query as follows;这样的一个例子是https://www.AjaxProxy.com可以与您的查询一起使用,如下所示;

ajaxProxy.proxy.url = "http://your proxy";
ajaxProxy.proxy.credentials.username = "proxy username";
ajaxProxy.proxy.credentials.password = "proxy password";
$.ajax({
    type: "GET",
    url: "https://ICANHAZIP.COM",
    headers: ajaxProxy.proxyHeaders(),
    dataType: "text"
}).done (function (data) {
    console.log(data);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM