Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making jQuery.ajax request through a proxy server

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.

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.

like image 283
Ziink Avatar asked May 03 '14 19:05

Ziink


People also ask

Is it possible to use jQuery with a HTTP proxy?

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. Show activity on this post. 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.

Which method is used to perform an Ajax request?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method.

Do I need a server-side proxy for Ajax?

However, you will always need a server-side proxy functionality. Show activity on this post. You would need an external library to perform Ajax requests via a HTTP Proxy using JQuery.

How are AJAX requests sent to the server?

By default, Ajax requests are sent using the GET HTTP method. If the POST method is required, the method can be specified by setting a value for the type option. This option affects how the contents of the data option are sent to the server.


2 Answers

[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 related methods which might help you with this.

My suggestion is to use jQuery.ajaxPrefilter and adapt your query to use the proxy instead of the original host. 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. For example to see if any of the requests fail:

$( document ).ajaxError(function() {
 console.log("Somethin' went wrawng!");
});
like image 118
Yan Foto Avatar answered Nov 09 '22 15:11

Yan Foto


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 web service of another origin,
  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.

like image 43
fjc Avatar answered Nov 09 '22 14:11

fjc