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 并且工作正常:
$.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 调用失败的响应标头
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
I tried a jquery get on a particular page on my forums
$.get("/members/",
function(data) {
alert(data);
});
but it would always return a blank page. However, if I use an xmlhttprequest object with my own code, it grabs the html page just fine:
xhr({method: "GET", url: "/members/",
onload: function(responseDetails) {
if (responseDetails.status == 200) {
alert(responseDetails.responseText);
}}});
Intestingly, I used the jquery get on another page and it worked fine:
$.get("/stats/",
function(data) {
alert(data);
});
In any case, could anyone explain what's going on and how I can fix this? I'd rather not have this long stupid section of code for my custom xhr method:
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);
}
Thanks! (Hopefully it's not a problem with how that forum page was set up, since it's a hosted forums, not my own.)
EDIT: response headers on xhr call
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
response headers on failed 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论