views:

88

answers:

4

Hi, I'm aware that this is kinda a hack but for reasons of simplicity I would like to replace the contents of an entire webpage with an answer received through Ajax. This includes possible HTTP headers. Is it possible to do so?

To clarify further, right now I added for debugging purposes:

alert(response);

and this produces:

example

For reasons I don't wanna get into I cannot do something like location.href = 'ajax_page'. Solutions that use jQuery would be better.

Code used to obtain the data:

$(document).ready(function() {
    new AjaxUpload('#upload', {
        action: '/AjaxSubmit',
        name: 'file',
        autoSubmit: true,
        onSubmit: function(file, extension) { return true; },
        onComplete: function(file, response) {
                alert(response);
        }
    });
});
A: 

I'm not sure what you mean by including HTTP headers.

From your screenshot, it looks like your server isn't setting the encoding correctly.

Solving this will depend on the page you're requesting and the web server you're using.

You should try requesting the URL in a new tab and/or in Fiddler, and you shouldn't debug it in the AJAX call until you can get the correct content in a browser tab.

SLaks
It's not the encoding, it's that it's compressed
Andreas Bonini
How is it compressed? If it's standard GZIP encoding, that ought to be handled transparently by the browser (AFAIK). If it isn't, this is by definition impossible, unless you write a decompresser in Javascript.
SLaks
It is deflated, like any other page on the website.
Andreas Bonini
+1  A: 

I don't think you can, not with HTTP headers. The reason is that the scope you have control over, the document, is created and rendered after the data is received.

Most of the HTTP headers affect the way data is transported and received. For example, by the time you get your hands on the response, it has already been uncompressed, character encoding has been applied etc.

Roland Bouman
A: 

The server has to take the following two things into account:

  • Write it as UTF-8.
  • Set Content-Type response header to text/html;charset=UTF-8.
BalusC
A: 

I'd use <a href=""></a>.

Ms2ger