两个异步AJAX调用返回相同的结果
Two async AJAX calls returns same result
我有两个ajaxcall,我叫它们async
:
xmlhttpPostInstagram2('firsturl');
xmlhttpPostInstagram3('secondurl');
问题是我从两个调用中得到相同的结果。如果我将async
改为同步,我将得到两个不同的结果,这是预期的结果。任何一点是什么搞乱了ajax async
调用?
我不想使用jquery
function xmlhttpPostInstagram2(strURL) {
var originalValue = ""
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
var temp2 = document.getElementById('sidebartag');
temp2.innerHTML = self.xmlHttpReq.responseText; // child is the fetched string from ajax call in your case
}
}
self.xmlHttpReq.send();
}
和
function xmlhttpPostInstagram3(strURL) {
var originalValue = ""
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
var temp2 = document.getElementById('sidebartag1');
temp2.innerHTML = self.xmlHttpReq.responseText; // child is the fetched string from ajax call in your case
}
}
self.xmlHttpReq.send();
}
不使用jQuery的说明
在你的例子中:
你同时调用这些函数:xmlhttpPostInstagram2('firsturl');
xmlhttpPostInstagram3('secondurl');
当你运行第一个函数xmlhttpPostInstagram2
时,你初始化了XMLHttpRequest
对象,同时,在第二个函数xmlhttpPostInstagram3
中,你覆盖了XMLHttpRequest
对象,因为第一个请求没有完成。
您可以尝试在每个函数中单独声明一个XMLHttpRequest
的实例。像这样:
function xmlhttpPostInstagram2(strURL) {
var xhr = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP");
xhr.open("POST", strURL, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
var temp2 = document.getElementById("sidebartag");
obj = JSON.parse(xhr.responseText);
temp2.innerHTML = obj.name;
}
};
xhr.send();
}
:
function xmlhttpPostInstagram3(strURL) {
var xhr = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP");
xhr.open("POST", strURL, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
var temp2 = document.getElementById("sidebartag1");
obj = JSON.parse(xhr.responseText);
temp2.innerHTML = obj.name;
}
};
xhr.send();
}
然后,您可以同时正确地运行这些函数:
xmlhttpPostInstagram2("http://api.openweathermap.org/data/2.5/weather?q=London");
xmlhttpPostInstagram3("http://api.openweathermap.org/data/2.5/weather?q=UK");
现场演示
我认为你的问题与范围有关。你有这样一行:
var self = this;
在函数的作用域中(从您提供的代码中我可以看出),this
指的是window
。因此self
等于window
。要检查这一点,在自我声明语句之后添加一个console.log(self)
,并在浏览器开发工具中检查结果。
var self = this;
console.log(self); // Logs Window object to console.
然后运行以下语句:
self.xmlHttpReq = new ...
当你这样做时,xmlHttpReq
的变量引用引用了你的两个函数(即window.xmlHttpReq
)中的相同变量。当您进行第二次函数调用时,该变量将被覆盖,这可以解释为什么看起来您从两次函数调用中获得相同的结果。
xmlHttp
声明为函数范围内的局部变量:
function xmlhttpPostInstagram2(){
var xmlHttpReq;
// Mozilla/Safari
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
//etc...
}
相关文章:
- JSONP请求返回结果,但也触发error_callback
- async问题,JS Promise无法返回结果,但可以使用console.log
- Meteor js-控制台显示'未定义'即使返回结果
- 正在等待HTTP调用从函数返回结果
- 使用函数for循环遍历对象以更改值,然后返回结果
- 迭代 JSON 文件并且未在 Javascript 中正确返回结果
- Javascript 函数不返回结果
- 用逗号分隔返回结果中的对象
- 需要呈现MongoDB查询返回结果的特定索引/位置
- HTML5 文件阅读器如何返回结果
- javascript循环遍历列表项,并在数组中返回结果
- 使用jquery UI next按钮提交输入字段值,php将返回结果
- 在angularjs工厂中,如何在函数解析后返回结果
- 我在jQuery中的Rock Paper Scissors程序只在平局时返回结果
- 如何使用ajax调用php并返回结果
- 添加第二维度的Javascript未返回结果
- Node.js没有返回结果
- jquery裁剪图像selction未返回结果
- 如何在phonegap javascript函数中返回结果
- 如果文本筛选器在ng重复中未返回结果,则显示消息