Ajax Talk - Asynchronous javascript and xml discussion board
 FAQ   Search    Usergroups      Register   Profile   Check your messages   Log in 
Signup now to enjoy a range of free membership advantages !!
Ajax Talk - Moderators Wanted
AJAX Object

 
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Programmers Exchange
View previous topic :: View next topic  
Author Message
MeTitus
New one
New one


Joined: 13 Sep 2006
Posts: 1
Location: London - Lisbon

PostPosted: Wed Sep 13, 2006 1:01 pm    Post subject: AJAX Object Reply with quote

Hi you all,

I am trying to code an AJAX object, that allows me to make multiple requests, but I am having some problems.

Here is the code

<code --------------------------------------------->


//////////////////////////////////////////////////////////
//
// OBJECT METHODS AND ATTRIBUTE
//
//////////////////////////////////////////////////////////

var ajaxArgs =
{
xmlHttp : "",
serverURL : "",
connectionArgs : "",
connectionType : "",
waitingMode : "",
serverAnswerHandler : "",
requestId : ""
};

//////////////////////////////////////////////////////////

function initProperties(serverURL, connectionArgs, connectionType, waitingMode, serverAnswerHandler, requestId)
{
ajaxArgs.serverURL = serverURL; //DATABASE URL
ajaxArgs.connectionArgs = connectionArgs; //QUERYSTRING
ajaxArgs.connectionType = connectionType; //POST/GET
ajaxArgs.waitingMode = waitingMode; //TRUE/FALSE
ajaxArgs.serverAnswerHandler = serverAnswerHandler; //
ajaxArgs.requestId = requestId; //
}

//////////////////////////////////////////////////////////

function openConnection()
{
if (navigator.userAgent.indexOf("MSIE")>=0)
{
var strName = "Msxml2.XMLHTTP";

if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName = "Microsoft.XMLHTTP";
}

try
{
ajaxArgs.xmlHttp = new ActiveXObject(strName);
}
catch(exception)
{
return 0;
}
}
else if (navigator.userAgent.indexOf("Mozilla")>=0)
{
try
{
xmlHttp = new XMLHttpRequest();
}
catch(exception)
{
return 0;
}
}
}

//////////////////////////////////////////////////////////

function doServerRequest()
{
try
{
ajaxArgs.xmlHttp.onreadystatechange = stateChanged;

if(ajaxArgs.connectionType.toUpperCase() == "POST")
{
ajaxArgs.xmlHttp.open(ajaxArgs.connectionType, ajaxArgs.serverURL + "?" + ajaxArgs.connectionArgs, ajaxArgs.waitingMode);
ajaxArgs.xmlHttp.send(null);
}
else if(ajaxArgs.connectionType.toUpperCase() == "GET")
{
ajaxArgs.xmlHttp.open(ajaxArgs.connectionType, ajaxArgs.serverURL, ajaxArgs.waitingMode);
ajaxArgs.xmlHttp.send(null);
}
else
{
return 0;
}

return 1;
}
catch(exception)
{
return 0;
}
}

//////////////////////////////////////////////////////////

function stateChanged()
{
if (ajaxArgs.xmlHttp.readyState==4 || ajaxArgs.xmlHttp.readyState=="complete")
{
if (ajaxArgs.xmlHttp.status == 200)
{
ajaxArgs.serverAnswerHandler(ajaxArgs.requestId,ajaxArgs.xmlHttp.responseText);
}
else
{
ajaxArgs.serverAnswerHandler(ajaxArgs.requestId,"error");
}
}
}


//////////////////////////////////////////////////////////
//
// CREATE AJAX OBJECT
//
//////////////////////////////////////////////////////////


function classAJAX(serverURL, connectionArgs, connectionType, waitingMode, serverAnswerHandler, requestId)
{
this.baseInitProperties = initProperties;
this.baseInitProperties(serverURL, connectionArgs, connectionType, waitingMode, serverAnswerHandler, requestId);
this.openConnection = openConnection;
this.doServerRequest = doServerRequest;
}

</code --------------------------------------------->



And here is how I use it:


<code --------------------------------------------->

var objAJAX = new classAJAX("testDatabase.aspx","section=0","post","true",serverData,"0");
objAJAX.openConnection();
objAJAX.doServerRequest();


var objAJAX1 = new classAJAX("testDatabase.aspx","section=1","post","true",serverDataa,"0");
objAJAX1.openConnection();
objAJAX1.doServerRequest();

function serverData(pId, serverData)
{
if(pId == 0)
alert("0");

if(pId == 1)
alert("0");
}


</code --------------------------------------------->


When I make one request everything goes just fine, so say I make a request using the argument 0, I get a message with 0 on it, the same applies to 1, but If I make one request, fallowed by another one, only the last request is processed, so instead of being shown two messages, 0 and 1, I just see the last one. Can be this cause because javascript only allows one instance of the native xmlttprequest object, I am saying this because it looks like this is what is happening.

Thanks in advance,

MeTitus
Back to top
View user's profile Send private message
evan_carothers
Light Frequent Poster
Light Frequent Poster


Joined: 11 Oct 2007
Posts: 9
Location: River Falls, WI

PostPosted: Thu Oct 11, 2007 10:15 pm    Post subject: Reply with quote

Where is you serverAnswerHandler function? It seems like you should get back both objects just fine, you just need to define how to process the result based on which of the initial requests the response is from. Right now both request are probably coming back, but it's process sucess for the first one, then the second one comes along and it does the same thing. You are giving them both the same requestID of 0 for some reason. If these were unique you could have an individual process (a switch statement or if statement) in the serverAnswerHandler function to do whatever you want specifically for the response with the matching requestID
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Programmers Exchange All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
 Advertisements
 XML & Copyright Notice 
    


Powered by phpBB © 2001, 2005 phpBB Group

What is according to Wikipedia: Ajax Web2.0 XML E4X


Link to our site:

  


del.icio.us digg spurl wists simpy newsvine blinklist furl blogmarks yahoo! myweb smarking ma.gnolia segnalo reddit fark technorati cosmos


Normal Bookmark


Partner Button Links:
Codecrunch.com: Webmaster Tutorials    One Ajax


Partner Text Links:
quomon | web20log | ajaxmatters | tableless | 456bereastreet | music charts | gift ideas | competitions | free xbox 360 | link trade ?


Credits:
it consultancy & project management lead generation zoekmachine optimalisatie steal these buttons seo expert