Need Newbie AJAX Help

LeetestUnleet

Senior member
Aug 16, 2002
680
0
0
This is my first attempt at anything AJAX, and it's mostly a learning exercise for me, but I plan to adapt the code to a real use. I followed the guide (and ripped most of the code from) here.

When I click the button, I get HTTP Status 1 (Loading), but it's never triggered again after that. :edit: Fixed that on accident

I'm sure it's something ridiculously simple for someone who knows what they're doing, but I'm new to this AJAX thing and just trying to learn it.

This is the page:
http://homncruse.ti.sabren.com/ajax/

:edit:
Nevermind... the Readystate started working just after I made this post when I tried to clean it up. I must've had some typo somewhere that I accidentally fixed.

:edit again:
Works in IE, but not in Firefox...

:edit 3:
Found some cleaner code for getHTTPObject(), but same symptoms.

index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
<title>Testing AJAX</title>
<script language="javascript" type="text/javascript">

var url = "test.php?param="; // The server-side script

function handleHttpResponse() {
alert("Reached HTTP Status " + http.readyState);
if(http.readyState == 4) {
// responseText is blank in Firefox?
alert(http.responseText);
document.getElementById("htmlOutput").innerHTML = http.responseText;
}
}

var isWorking = false;

function getHTTPObject() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
try { return new XMLHttpRequest(); } catch(e) {}
alert("XMLHttpRequest not supported");
return null;
}

var http = getHTTPObject(); // We create the HTTP Object

function jsDoCR() {
if (!isWorking && http) {
var passText = "passText";
http.open("GET", url + escape(passText), true);
http.onreadystatechange = handleHttpResponse;
isWorking = true;
http.send(null);
}
}
</script>
</head>

<body>

<form method="post">
<button onClick='jsDoCR()'>Go go Gadget AJAX!</button><br/><br/>
</form>

<div id="htmlOutput" style="border: 1px solid black">This text should be replaced with output from test.php.</div>

</body>
</html>

test.php
<?php
echo "Foobar";
?>
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
try this for your getHttpObject() function:

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;

i think the problem is with the way you structure your try-catch statements...
 

LeetestUnleet

Senior member
Aug 16, 2002
680
0
0
Nope. No change. It appears like it's trying to do something, and my alert boxes notifying me of the HTTP readyState Changes are working, but when it finally hits readyState==4, http.responseText is blank.

Found another bug too. It pretends to work in IE, but it seems like it's all cached because it only updates on the first button click. Additional button clicks don't do anything (I've updated my local copy to include a textarea field to use as passText).
 

fs5

Lifer
Jun 10, 2000
11,774
1
0
Originally posted by: dandragonrage
Use a library for AJAX. I use Scriptalicious.
Use extjs.com, build your own custom one on their website or use the connection manager from YUI.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Like many people have said, use one of those JS libraries. It's fine to roll your own stuff, but these libraries are a lot more reliable to use in production since they are much better tested.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |