9 个答案:

答案 0 :(得分:31)

按如下方式配置jQuery ajax设置:

$.ajaxSetup({

error: handleXhrError

});

其中handleXhrError函数如下所示:

function handleXhrError(xhr) {

document.open();

document.write(xhr.responseText);

document.close();

}

另见:

答案 1 :(得分:2)

在你的ajax回调中:

success: function (data) {

$("html").html($(data).find("html").html());

}

这将用从AJAX请求收到的内容替换整个页面的HTML内容。适用于Chrome ...不确定IE。

尽管如此,我不确定你为什么要包含

部分......但是你可以轻松地修改上面的部分来显示AJAX响应体内的内容,并附加它到一个div甚至一个灯箱。好多了。

答案 2 :(得分:2)

以下是如何更改响应是url还是html内容(使用django \ php)的示例

var xmlhttp;

xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange=function()

{

var replace_t = '{{ params.replace_t }}';

if (xmlhttp.readyState==4 && xmlhttp.status==200)

{

if(replace_t == 'location')

window.location.replace(xmlhttp.responseText);

else if(replace_t == 'content')

{

document.open();

document.write(xmlhttp.responseText);

document.close();

}

}

}

xmlhttp.open("GET",SOME_ASYNC_HANDLER_URL,true);

xmlhttp.send();

答案 3 :(得分:2)

您也可以尝试使用数据网址,支持它的主流浏览器的最新版本:

function utf8_to_b64( str ) {

return window.btoa(unescape(encodeURIComponent( str )));

}

function loadHtml(html)

{

localtion.href='data:text/html;base64,'+utf8_to_b64(html);

}

这样,您可以在运行时加载任何所需的html页面。

答案 4 :(得分:1)

我找到了这个解决方案。我不知道它是否正确,但对于Opera和Firefox来说它是有效的。

var error_win = window.open(

'',

'Server error',

'status=0,scrollbars=1, location=0'

);

error_win.document.write(XMLHttpRequest.responseText);

答案 5 :(得分:0)

您是否尝试过简单地创建一个元素并将返回的错误页面插入元素中?我使用错误页面和jQuery执行此操作。

var errorContainer = $( '

errorContainer.html( errorTextResponse );

errorContainer.appendTo( $( 'body' ) );

答案 6 :(得分:0)

我可能会误解,但你知道你特意要显示的结果中的哪些元素?你可以尝试这样的事情:

success: function(data){

//store the response

var $response=$(data);

//use .find() to locate the div or whatever else you need

var errorMessage = $response.find('#warning').text();

alert(errorMessage);

}

那是你在找什么?

答案 7 :(得分:0)

我认为没有办法做到这一点。 iframe用于加载其他页面,并且没有其他沙箱可以转储独立页面 - 这就是为其设计的框架。

使用您正在使用的框架可能很困难,但是为您的Ajax请求生成不同的错误可能是值得的。我的Ajax页面只会发送

{"exit": 1, "message": "As in the shell, a non-zero exit is an error and this is why..."}

答案 8 :(得分:0)

刚想出来了

就像一样简单

document.body.innerHTML = YourAjaxrequest.responseText;

_______________________________________________ ^这里是用你的回复写你当前的HTML页面的内容。

request.onreadystatechange = function() {

if (request.readyState == 1) {

document.getElementById('sus').innerHTML = "SENDING.......";

}

if (request.readyState == 3){

document.getElementById('sus').innerHTML = "SENDING >>>>>>>>>>>>>";

}

if (request.readyState == 4 && request.status == 200) {

//document.getElementById('sus').innerHTML = request.responseText;

document.body.innerHTML = request.responseText;

}

}

request.send(formD);

},false);

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐