Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 04-16-2007, 04:23 AM an ajax question
Skilled Talker

Posts: 60
Name: tami
Trades: 0
i have an ajax code that works and loads an asp file but since i dont want to deal with asp i want it to get the infrormation from a simle txt file . the code that refers to the asp is this:
Code:
 xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET","some.asp",true);
xmlHttp.send(null);
so if i replace the asp with a simple txt nothing happens, what is the correct way to write the txt?
below is all the code:
Code:
<script type="text/javascript">
function showText(){
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
 xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET","some.asp",true);
xmlHttp.send(null);
  }
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;
}
function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("myText").innerHTML=xmlHttp.responseText;
}
} 
  showText();
</script>
</head>
<body>
my text: <div id="myText"></div>  
</body>
</body>
</html>
pizza is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-16-2007, 11:51 AM Re: an ajax question
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
This code is a bit confusing, but let me get you started. Your xmlHttp variable is defined only within functions so it cannot be cross-accessed by functions (it goes out of scope). Instead, declare xmlHttp outside of all functions and then they'll share the variable's values. Next, you have
xmlHttp=GetXmlHttpObject() and within GetXmlHttpObject you start declaring xmlHttp again before returning it at the end. I have no idea what that kind of referencing will result in, but you probably want to try cleaning up the variable names.

That said, let me give you what we use on our site:

Code:
var request = false;
var requested_page = null;

function ajaxSend(url,data,form_type) {
  if (form_type.length == 0) {
    form_type = "GET";
  }
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request) {
    //Error
  }
  else {
    //ajax commands available.

    if (form_type == "POST") {
      request.open(form_type, url, true);
      request.onreadystatechange = ajaxReceive;
      request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      request.send(data+"&ajax_call=true");
    }
    else {
      request.open(form_type, url+"?"+data+"&ajax_call=true", true);
      request.onreadystatechange = ajaxReceive;
      request.send(null);
    }
    requested_page = data;
  }
}
function ajaxReceive() {
  if (request.readyState == 4) {
    if (request.status == 200) { //everything went smoothly
      var response = request.responseText;
      receivePage(response);
    }
    // else an error accessing the URL.
  }
  // else still processing, so do not execute routine
}
To use, just call ajaxSend(url,data,form_type). For example, ajaxSend("some.asp","","GET").

Then, declare a function called receivePage that accepts one parameter - the return value - and performs the proper parsing. For example,

Code:
function receivePage(serverResponse) {
  document.getElementById("myText").innerHTML=serverResponse;
}
As a last note, the code I gave you for our site does a bit more than you want but that's because it helps us to be able to determine if a call is ajaxian or not.

Hope this helps!
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to an ajax question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27616 seconds with 12 queries