简体   繁体   English

通过Ajax替换整个网页的内容

[英]Replacing the contents of an entire webpage through ajax

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. 我知道这是一种破解,但出于简单起见,我想将整个网页的内容替换为通过Ajax收到的答案。 This includes possible HTTP headers. 这包括可能的HTTP标头。 Is it possible to do so? 有可能这样做吗?

To clarify further, right now I added for debugging purposes: 为了进一步说明,现在我出于调试目的添加了:

alert(response);

and this produces: 这会产生:

example http://img195.imageshack.us/img195/6362/testbzi.png 示例http://img195.imageshack.us/img195/6362/testbzi.png

For reasons I don't wanna get into I cannot do something like location.href = 'ajax_page' . 由于某些原因,我不想进入,因此无法执行location.href = 'ajax_page' Solutions that use jQuery would be better. 使用jQuery的解决方案会更好。

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);
        }
    });
});

I don't think you can, not with HTTP headers. 我认为您无法使用HTTP标头。 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. 大多数HTTP标头都会影响数据的传输和接收方式。 For example, by the time you get your hands on the response, it has already been uncompressed, character encoding has been applied etc. 例如,当您获得响应时,响应已被解压缩,已应用字符编码等。

I'm not sure what you mean by including HTTP headers. 我不确定您包含HTTP标头的意思。

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. 解决方案将取决于您请求的页面和所使用的Web服务器。

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. 您应该尝试在新选项卡和/或Fiddler中请求URL,并且除非可以在浏览器选项卡中获得正确的内容,否则不要在AJAX调用中对其进行调试。

The server has to take the following two things into account: 服务器必须考虑以下两件事:

  • Write it as UTF-8. 将其编写为UTF-8。
  • Set Content-Type response header to text/html;charset=UTF-8 . Content-Type响应标头设置为text/html;charset=UTF-8

我会使用<a href=""></a>

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

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