替换当前页面,Ajax内容 [英] Replace current page with ajax content
问题描述
我有一个对话窗口,它发送AJAX POST数据到服务器并接收响应的页面。在开发过程中,可以有两种反应 - 一个常规(这不是问题),或者一个一个错误。服务器返回code 500和大量的调试信息的页面。这是一个框架,返回正常的网页,并包含一些JavaScript code。我希望能够在情况下,它会发生显示错误页面。
I have a page with a dialog window which sends ajax post data to server and receives a response. During development, there can be two responses - one regular (this is not the question) or one with an error. Server returns code 500 and a page with lot of debug informations. This is a regular page returned from a framework and contains some javascript code. I want to be able to display this error page in case it happens.
现在的问题是,我不能简单地将返回的结果为body元素或打开一个新页面一个新的链接,然后重新装入此错误。我只是得到一个HTML页面,而不是数据,我要显示的页面(在当前窗口或另外一个)。
The problem is, I can not simply attach the returned result to body element or open a new link in a new page and load this error again. I simply get a html page instead of data and I have to display the page (in current window or in another one).
我使用jQuery。
感谢您的任何建议!
推荐答案
配置jQuery的阿贾克斯设置如下:
Configure jQuery ajax setup as follows:
$.ajaxSetup({
error: handleXhrError
});
其中, handleXhrError
函数是这样的:
function handleXhrError(xhr) {
document.open();
document.write(xhr.responseText);
document.close();
}
参见:
- <一个href="http://stackoverflow.com/questions/1738785/handling-of-server-side-http-4nn-5nn-errors-in-jquerys-ajax-requests">Handling服务器端的HTTP 4NN / 5NN jQuery中 错误
- Handling of server-side HTTP 4nn/5nn errors in jQuery
See also:
这篇关于替换当前页面,Ajax内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!