ajax提交成功后变成空白页,JQuery Ajax返回空白页但手动xmlhttprequest工作正常

我在论坛上尝试了一个jquery get特定页面

$.get("/members/",

function(data) {

alert(data);

});

但它总会返回一个空白页面。但是,如果我将xmlhttprequest对象与我自己的代码一起使用,它就可以很好地抓取html页面:

xhr({method: "GET", url: "/members/",

onload: function(responseDetails) {

if (responseDetails.status == 200) {

alert(responseDetails.responseText);

}}});

在我看来,我在另一个页面上使用了jquery,它工作得很好:

$.get("/stats/",

function(data) {

alert(data);

});

在任何情况下,任何人都可以解释发生了什么以及如何解决这个问题?对于我的自定义xhr方法,我宁愿没有这个冗长的代码部分:

function xhr(details) {

try {var xmlhttp = new XMLHttpRequest();} catch (e) {

var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' );

var success = false;

for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {

try {

var xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);

success = true;

} catch (e) {}

}

if (!success) throw new Error('Unable to create XMLHttpRequest.');

}

xmlhttp.onreadystatechange = function() {

var responseState = {

responseText:(xmlhttp.readyState==4 ? xmlhttp.responseText : ''),

readyState:xmlhttp.readyState,

status:(xmlhttp.readyState==4 ? xmlhttp.status : 0)

}

if (details["onreadystatechange"]) {

details["onreadystatechange"](responseState);

}

if (xmlhttp.readyState==4) {

if (details["onload"] && xmlhttp.status>=200 && xmlhttp.status<300) {

details["onload"](responseState);

}

if (details["onerror"] && (xmlhttp.status<200 || xmlhttp.status>=300)) {

details["onerror"](responseState);

}

}

}

try {

xmlhttp.open(details.method, details.url);

} catch(e) {

if( details["onerror"] ) {

details["onerror"]({responseXML:'',responseText:'',readyState:4,responseHeaders:'',status:403,statusText:'Forbidden'});

}

return;

}

if (details.headers) {

for (var prop in details.headers) {

xmlhttp.setRequestHeader(prop, details.headers[prop]);

}

}

xmlhttp.send((typeof(details.data)!='undefined')?details.data:null);

}

谢谢! (希望这个论坛页面的设置方式不是问题,因为它是托管论坛,而不是我自己的论坛。)

编辑:xhr调用的响应标头

Date Sat, 30 Apr 2011 01:21:54 GMT

Server Apache

Cache-Control no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0

Pragma no-cache

Expires Mon, 26 Jul 1997 05:00:00 GMT

Content-Encoding gzip

Vary Accept-Encoding

Content-Length 7723

Connection close

Content-Type text/html; charset=utf-8

失败的jquery get call 上的

响应标头Date Sat, 30 Apr 2011 01:23:38 GMT

Server Apache

Content-Encoding gzip

Vary Accept-Encoding

Content-Length 26

Connection close

Content-Type text/html; charset=ISO-8859-1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值