Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do you have multiple ajax requests on the same page using ASP?

Posted on 2009-05-18
14
Medium Priority
?
415 Views
Last Modified: 2012-05-07
I'm making a page to query other website and return a result.

Like this website does
http://tools.web-max.ca

I know how to do this for one website using MSXML2.ServerXMLHTTP.

The remote page would just display "success" or "fail"

So one the main page e.g

Name             status
website 1      success
website2       fail

The code I'm using at the moment can only handle one request at a time, how do I get more to display?
<script>
var xmlHttp
 
function vremote(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
  document.getElementById('loading').style.visibility='visible';
var url="test.asp";
var testurl = document.getElementById('testurl').value;
url=url+"?testurl="+testurl;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
document.getElementById('loading').style.visibility='hidden';
}
}
 
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
 
</script>

Open in new window

0
Comment
Question by:djpazza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
14 Comments
 
LVL 25

Accepted Solution

by:
kevp75 earned 1000 total points
ID: 24420985
pass the div ID to the function as well...  and then have multiple divs on your page:


<script>
var xmlHttp
 
function vremote(str, divToHold)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
  document.getElementById('loading').style.visibility='visible';
//var url="test.asp";
//var testurl = document.getElementById('testurl').value;
//url=url+"?testurl="+testurl;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",str,true);
xmlHttp.send(null);
}
function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById(divToHold).innerHTML=xmlHttp.responseText;
document.getElementById('loading').style.visibility='hidden';
}
}
 
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
 
</script>
 
 
Assuming str is supposed to be the URL:
 
<div id="div1" onclick="vremote('http://www.google.com', this.id);">Click to load Google</div>
<div id="div1" onclick="vremote('http://www.yahoo.com', this.id);">Click to load Yahoo</div>

Open in new window

0
 
LVL 25

Expert Comment

by:kevp75
ID: 24420993
sorry...change this:
<div id="div1" onclick="vremote('http://www.google.com', this.id);">Click to load Google</div>
<div id="div1" onclick="vremote('http://www.yahoo.com', this.id);">Click to load Yahoo</div>

to:
<div id="div1" onclick="vremote('http://www.google.com', this.id);">Click to load Google</div>
<div id="div2" onclick="vremote('http://www.yahoo.com', this.id);">Click to load Yahoo</div>
0
 
LVL 9

Author Comment

by:djpazza
ID: 24421436
thanks kevp75 just tried the script and its not working, I have tried something this before but I'm guessing you can not pass the divToHold to the next function?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:kevp75
ID: 24421595
What next function?   gimme a few, I'll get a test page up for ya, and some new code
0
 
LVL 25

Expert Comment

by:kevp75
ID: 24421676
Please see the code below:
test page: http://www.zipcm.com/temp.asp

please note: you may not be able to pull this from another domain, as you would not have proper access in order to do it.
/temp.asp
<script type="text/javascript">
//Get Contents of another page, and display them inside an element
strTargetDiv = '';
function loadurl(dest, str1) {
     strTargetDiv = str1;
     try {
     xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
     new ActiveXObject("Microsoft.XMLHTTP");
     }
     catch (e) { /* do nothing */ }
	 if (dest.indexOf("?")>0){
	     dest = dest + "&sid="+Math.random()
	 } else {
	     dest = dest + "?sid="+Math.random()
	 }
     xmlhttp.onreadystatechange = triggered;
     xmlhttp.open("GET", dest);
     xmlhttp.send(null);     
}
function triggered() {
     if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var txt = xmlhttp.responseText;
		var txtNod = document.createTextNode(txt);
		document.getElementById(strTargetDiv).style.display = '';
		document.getElementById(strTargetDiv).innerHTML = txt;
     }
}
</script>
<div id="div1" onclick="loadurl('/temp2.asp', this.id);" style="cursor:pointer;">Click to load time in this div from /temp2.asp</div>
<div id="div2" onclick="loadurl('/temp2.asp', this.id);" style="cursor:pointer;">Click to load time in this div from /temp2.asp</div>
 
 
/temp2.asp:
<%
Response.Write(Now())
%>

Open in new window

0
 
LVL 25

Expert Comment

by:kevp75
ID: 25469592
I would like to know what the questioner came up with for "I have made a solution to my question"

24421676 is a valid and working answer
0
 
LVL 9

Author Comment

by:djpazza
ID: 25476958
Thanks for you code kevp75, did try it but as you indicated I could not get it to work getting information from other domains, which is one of the requires in my question.
0
 
LVL 25

Expert Comment

by:kevp75
ID: 25477602
Well...  if you have control over the other domain(s), you can pass credentials through this effectively logging in and getting access to do what you need.

Although, in your question you do not mention the need to retrieve information from either, just a pass or fail.

Using the code I posted (with a little modification) will do that for you.  All that needs to be done is an else statement in the triggered triggered function to return a false value will do that.

I'll get another test page up and code for this.  (your question...)
0
 
LVL 25

Expert Comment

by:kevp75
ID: 25477796
test page:

http://development.zipcm.com/testing.asp

code:

/testing.asp:
 
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
//Get Contents of another page, and display them inside an element
strTargetDiv = '';
function loadurl(dest, str1) {
     strTargetDiv = str1;
     try {
     xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
     new ActiveXObject("Microsoft.XMLHTTP");
     }
     catch (e) { /* do nothing */ }
         if (dest.indexOf("?")>0){
             dest = dest + "&sid="+Math.random()
         } else {
             dest = dest + "?sid="+Math.random()
         }
     xmlhttp.onreadystatechange = triggered;
	 try{
	     xmlhttp.open("GET", dest);
    	 xmlhttp.send(null);     
		 }catch(err){
		 	document.getElementById(strTargetDiv).innerHTML = 'Fail';
		 }
}
function triggered() {
     if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
                var txt = xmlhttp.responseText;
                var txtNod = document.createTextNode(txt);
                document.getElementById(strTargetDiv).style.display = '';
                document.getElementById(strTargetDiv).innerHTML = 'Success';
     }else{
	 	document.getElementById(strTargetDiv).innerHTML = 'Fail';
	 }
}
</script>
</head>
 
<body>
<div id="div1" onclick="loadurl('/testing2.asp', this.id);" style="cursor:pointer;">Click to load time in this div from /testing2.asp</div>
<div id="div2" onclick="loadurl('http://www.yahoo.com', this.id);" style="cursor:pointer;">Click to load yahoo.com in this div</div>
 
</body>
</html>
 
/testing2.asp
<%
Response.Write(Now())
%>

Open in new window

0
 
LVL 9

Author Comment

by:djpazza
ID: 25499724
Thanks for the updated code however it's still not quite what i'm after. When I said the remote page would display success or fail this would be what text is on the remote page.  The idea is to create a webpage that tells me if a client backup has run or not which would be remotely hosted on a clients server.  Then from a webpage in my office I can setup and query lots of clients backups in one screen.  The example url I provided has been changed now but what it did on load is go away and then change the icon against a name too a tick or cross which is basically what im after depending on the remote text.

I have tried the above method but can not get it to pull information from an external domain.
0
 
LVL 25

Expert Comment

by:kevp75
ID: 25500184
>>
I have tried the above method but can not get it to pull information from an external domain.<<

as has already been stated, this cannot be done across multiple domains unless you are authenticated.  I suggest you do a search on "Cross Site Scripting" to see why it is not allowed.

The code I posted does what you asked it do to...  pass back a Success or Fail...
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 1000 total points
ID: 25532257
If you need to check a site on a different domain then you need to look at server script as the way to get it.  In other words you would use a page on your server to get the response from the remote site.  Then you could still use AJAX to dynamically fill the page but the URL would be a page on your server.  The same server as the page with the AJAX script.  Kevp75 is right that you can't access other domains, etc with AJAX.  He gave you the term for it and it would pertain to a different protocol as well.  It is browser security to protect the user but can be done by server script.

If your server language is ASP then it doesn't have a great tool to use for this but the Server version of the MS XMLHTTP object or third party programs like ASPTear or ASPHTTP.

Let us know how this helps or if you have a question.

bol
0
 
LVL 9

Author Closing Comment

by:djpazza
ID: 31639150
I have written my own asp solution based on kevp75's comment about passing the id to a function.  Using MSXML2.ServerXMLHTTP I have written a vb script function that does the same job.  Apologies for the confusion as I should have posted the MSXML2.ServerXMLHTTP code instead of the xmlHttp=GetXmlHttpObject code.  Getting information from a remote website was a core part of the question. Thanks for your help with this query.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question