gpt4 book ai didi

javascript - 从 Ajax 调用获取响应

转载 作者:行者123 更新时间:2023-11-28 20:22:59 25 4
gpt4 key购买 nike

当尝试从普通 JavaScript 中内置的 ajax 调用获取responseText 时,Firebug 似乎看到了该请求,但无法获取对responseText 的引用。

这是函数的代码

function getAjaxResponse(){    
var ajaxObj = getAjaxObj();
ajaxObj.open('get', 'responsePage.php', true);
ajaxObj.onReadyStateChanged = function(){
if(ajaxObj.readyState == 4
&& ajaxObj.status == 200){
//no functions are getting fired in here
//this does not get logged to console
console.log(ajaxObj.responseText);
//neither does this
console.log(2);
}
};
ajaxObj.send(null);

//this does gets logged to console
console.log(1);
}

ajax 对象的函数

function getAjaxObj(){
var req;
if(window.XMLHttpRequest){
try{
req = new XMLHttpRequest();
} catch(e){
req = false;
} finally {
return req;
}
} else {
if(window.ActiveXObject){
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e){
try{
req = new ActiveXObject("Msxml.XMLHTTP");
} catch(e){
req = false;
} finally {
return req;
}
}
}
}
}

这也是 firebug 的 View enter image description here

如何获取 ajax 调用响应的引用?

最佳答案

OnReadyStateChanged 需要为 onreadystatechange。 JavaScript 区分大小写。

关于javascript - 从 Ajax 调用获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17936158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com