Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timing issue with displaying data (ajax & json)

Status
Not open for further replies.

onressy

Programmer
Mar 7, 2006
421
CA
Hi, I'm trying something and it only works if i refresh the page, no errors. so i think there is a timing issue, i've been playing with it for a while trying to fix the timing issue with delays here or there but nothing seems to work.
I'm trying to put together a drop box from a remote server based from values from a database on a local server. using ajax to set a variable from ASP (asp calls the db for the values and sets it to a string) then using json to fromulate and display the drop box on the local server


I have a page call test.html (on remote server):
Code:
<html>
 <head>
<script src="[URL unfurl="true"]http://www.company.com/json.js"[/URL] type="text/javascript"></script>
  <title></title>
 </head>
 <body>
<script type="text/javascript">

jsonObj.passVar();

//function wait(delay) { 
//string="pauseforalert("+delay+");";
//setTimeout(string,delay); 
//}
//function pauseforalert(delay) { 
//jsonObj.passVar(); 
//}
//wait(5000);
//alert(a2);
</script>

<h1>hope this works!</h1>

 </body>

</html>

json.js (file resides on local server):
Code:
var a1="";


function a12() {
var xmlHttp;
  try
    { xmlHttp=new XMLHttpRequest(); }
  catch (e)
    { try
      { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e)
      { try
        { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e)
        { alert("Your browser does not support this search box. Try Firefox, Safari or IE"); return false; }  } }
    xmlHttp.onreadystatechange=function() 
{ 

if(xmlHttp.readyState==4) { a1 = xmlHttp.responseText; } 

}
    xmlHttp.open("GET","default.asp",true);
    xmlHttp.send(null);  

	return a1;
}



jsonObj = {
 'formA' : '<select class="frmRateGroup" name="PropertyL" ID="frmRateGroup"><option value="999">All</option>',
 'formB' : a12();,
 'formC' : '</select>',
 'website' : {
  'uri' : '[URL unfurl="true"]http://company.com/',[/URL]
  'stuff' : {
   'CSS' : 'Cascading Style Sheets',
   'HTML' : 'HyperText Markup Language',
   'JS' : 'JavaScript'
  },
  'passVar' : function() {
  document.write(this.formA + this.formB + this.formC);
  }
 },
 'passVar' : function() {
  document.write(this.formA + this.formB + this.formC);
 }
};

Whats happing in the code is that i'm pulling some info from a db using ajax & asp, i'm returning the asp string (which is actually formatted as html drop box options. Then i'm using the ajax returned variable to create a form element using json and display the json object on a remote server test.html in a form. All is working, no errors or any type, but the drop box does not contain the options from the ajax string until i refresh the page. When i refresh test.html after it loads, the drop box in test.html is populated with the option from ajax/asp. Any thought on how to make this work without refreshing the page once it loads? Thanks
 
I am not sure I understand exactly what you are doing but let me give it a shot.

You are using your JS functions to execute a call to an ASP page and that ASP page returns an ASP value?

Server-side script is executed first and any output of the server-side script is made part of the HTML that will be rendered in the browser. No javascript will execute until after the server-side code has already completed (unless your using server-side javascript?) and by the time javascript begins executing no server-side ASP scripting is going to execute.
So if your javascript is making an xmlHTTP request to an ASP page, that page can return any information you like but it will not execute any ASP code returned because you are now working in client-side script, not server-side.

You would need to use client-side script to take the response info from the ASP page and write it into your select box either by putting that select into a div or span tag and using it's innerHTML property to rewrite the whole select or by using DOM methods on the select box to alter it's available options.


At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top