[英]Multiple Ajax Call override values without jquery
I am having trouble calling multiple ajax calls together. 我无法同时调用多个ajax调用。 I am calling following three
getXValues
functions on page load to set three set of values by changing dome values using setXValues
. 我在页面加载时调用以下三个
getXValues
函数,以通过使用setXValues
更改圆顶值来设置三组值。
In this condition only one function executes and set same value in all three fields. 在这种情况下,只有一个函数执行并在所有三个字段中设置相同的值。
If i call each following function in previous setXValues
values then everything works fine this results in some delay where previous function has to be complete by calling setXValues
. 如果我在先前的
setXValues
值中调用以下每个函数,则一切正常,这会导致某些延迟,其中必须通过调用setXValues
来完成先前的函数。
function getIValues(){
var url = "./Servlet?ajaxAction=getIDetail&id="+id;
ajaxRequestForChange(url,"I");
}
function getFValues(){
var url = "./Servlet?ajaxAction=getFDetail&id="+id;
ajaxRequestForChange(url,"F");
}
function getCValues(){
var url = "./Servlet?ajaxAction=getCDetail&id="+id;
ajaxRequestForChange(url,"C");
}
var ajaxRequestForChange = function(url,fun) {
var strURL = url;
var xmlHttpRequest = false;
var self = this;
// Mozilla, Safari
if (window.XMLHttpRequest) {
self.xmlHttpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
self.xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpRequest.open("POST", strURL, true);
self.xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
self.xmlHttpRequest.onreadystatechange = function() {
if (self.xmlHttpRequest.readyState == 4) {
if (self.xmlHttpRequest.status == 200) {
var htmlString = self.xmlHttpRequest.responseText;
if(fun == 'I'){
setIValues(htmlString);
}
if(fun == 'F'){
setFValues(htmlString);
}
if(fun == 'C'){
setCValues(htmlString);
}
} else {
ajaxFailedCount++;
// Try for 1 min (temp fix for racing condition)
if (ajaxFailedCount < 1200) {window.setTimeout(function() {ajaxRequest(url)}, 50);}
else {alert("Refresh failed!")};
}
}
}
self.xmlHttpRequest.send(null);
}
It will be help full if you can provide approach which will resolve my problem. 如果您能提供解决我的问题的方法,那将对您有很大帮助。
Note: Please also note that I can't use JQuery since its not part of ui component (I hate it but no choice..) 注意:还请注意,我不能使用JQuery,因为它不是ui组件的一部分(我讨厌它,但别无选择..)
Thanks in advance for your help. 在此先感谢您的帮助。
You problem is one of scope and the context of this
. 您的问题是
this
问题的范围和背景之一。 In your function: 在您的职能:
var ajaxRequestForChange = function(url,fun) {
var strURL = url;
var xmlHttpRequest = false;
var self = this;
...
You assign var self = this;
您分配
var self = this;
However the function ajaxRequestForChange
is always running in the global scope. 但是,功能
ajaxRequestForChange
始终在全局范围内运行。 So later on when you do: 因此,稍后在您执行以下操作时:
if (window.XMLHttpRequest) {
self.xmlHttpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
self.xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpRequest.open("POST", strURL, true);
...
You are using the same obj self
for each call to ajaxRequestForChange
and overwriting the property xmlHttpRequest
every time. 您对
ajaxRequestForChange
每次调用都使用相同的obj self
,并且每次都覆盖xmlHttpRequest
属性。 Remove the var self = this;
删除
var self = this;
and remove self.
并去除
self.
from all lines that use it and that should fix your problem. 所有使用它的行都应该解决您的问题。 So the above becomes:
因此,以上内容变为:
var ajaxRequestForChange = function(url,fun) {
var strURL = url;
var xmlHttpRequest = false;
...
and 和
if (window.XMLHttpRequest) {
xmlHttpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpRequest.open("POST", strURL, true);
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.