我的 Javascript 函数请求到 aspx 页面。 代码:
var xhr = ("XMLHttpRequest" in window) ? new XMLHttpRequest() : new ActiveXObject("Msxml3.XMLHTTP");
xhr.open("GET", = 'http://www.example.net/abc.aspx', true);
xhr.send("");
在此请求之后,我想从此页面发回响应并在客户端捕获它。我该怎么做?
最佳答案
要在异步模式下从 XMLHttpRequest
获取响应(open()
方法的第三个参数为 true
),您必须设置onreadystatechange
属性到回调函数。当浏览器准备好响应时,将回调此函数:
xhr.open("GET", 'http://www.example.net/abc.aspx', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var serverResponse = xhr.responseText;
}
};
xhr.send(null);
您可能需要查看以下文章以进一步阅读该主题:
关于javascript - 在 Javascript 请求后使用 Javascript 从服务器获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3193976/