Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-01-2007, 12:56 PM   PM User | #1
santrooper
New to the CF scene

 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
santrooper is an unknown quantity at this point
Angry Ajax beginner in trouble

Hello all,
I have just strted learning ajax, I am trying to implement it using the object oriented model, but it is not working.....
I am submiting my code below
Please help, any body please do........
------------
ajaxObject.js
------------
//==JavaScript Document==//
function sanAjax(){
var xmlHttpRequestObj=null;

////////////////////////////////////////////////////////
this.xmlHttpRequestInit=function(){
var xmlhttpObj=null;
try{
// Firefox, Opera 8.0+, Safari
xmlhttpObj=new XMLHttpRequest();
}
catch (e){
// Internet Explorer
try{
xmlhttpObj=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
xmlhttpObj=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
alert('Your brwser is VERRRRRRY OLDDDDD !! Please let it resr and get a new one.');
return(false);
}
}
}
return xmlhttpObj;
}
//////////////////////////////////////////////////////
this.actionOnStateChange=function() {
alert(this.xmlHttpRequestObj);
if(this.xmlHttpRequestObj.readyState == 4){
if(this.xmlHttpRequestObj.status >= 400 && this.xmlHttpRequestObj.status < 500){
alert('sdsdsdsdsd');
}
else if(this.xmlHttpRequestObj.status >= 500 && this.xmlHttpRequestObj.status < 600){
alert('yyyyyyyyyyyyyyyyyyyy');
}
else{
alert('Helloooooooooooooooooo');
//document.getElementById(targetid).innerHTML=this.xmlHttpRequestObj.responseText;
}
}
}
//////////////////////////////////////////////////////
this.sanAjaxInit();
}
/////////////////////////////////////////////////////////////////
sanAjax.prototype.sanAjaxInit=function(){
this.xmlHttpRequestObj=this.xmlHttpRequestInit();

if(this.xmlHttpRequestObj == null){
alert("Your browser is not supporting Ajax!");
return;
}

if(this.xmlHttpRequestObj.overrideMimeType){
this.xmlHttpRequestObj.overrideMimeType('text/xml');
}
}
/////////////////////////////////////////////////////////////////
sanAjax.prototype.sendRequests=function(){
if (this.xmlHttpRequestObj==null){
alert("Browser does not support HTTP Request");
return;
}
else{
alert(this.xmlHttpRequestObj);
this.xmlHttpRequestObj.onReadyStateChange=this.actionOnStateChange;
}
this.xmlHttpRequestObj.open("GET",'index.php',true);
//ajaxObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
this.xmlHttpRequestObj.send(null);
}
---------
index.php
---------
<html>
<head>
<title>::Ajax Test::</title>
<script language="javascript" type="text/javascript" src="ajax_includes/ajaxObject.js"></script>
<script language="javascript" type="text/javascript">
var xmlHttp=null;
function createAjaxObj(){
//xmlHttp=new ZaxasRequest();
xmlHttp=new sanAjax();
}
function changeContent(){
xmlHttp.sendRequests();
}
</script>
</head>
<body onLoad="createAjaxObj()">
<div id="contDiv">
<form name="subNam" id="subNam">
<input type="text" name="cNam" id="txtname">
<input type="button" onClick="changeContent()" value="send">
</form>
</div>
</body>
</html>
santrooper is offline   Reply With Quote
Old 08-01-2007, 09:16 PM   PM User | #2
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
One error is onreadystatechange does not have capital letters.
You may have more errors, but that's a start.
rwedge is offline   Reply With Quote
Old 08-02-2007, 03:34 PM   PM User | #3
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
As already mentioned, you have camel case for onreadystatechange when it should all be lower.

Code:
this.xmlHttpRequestObj.onReadyStateChange=this.actionOnStateChange;
I have a feeling you also would need to use a closure:

Code:
var this_ = this;
this.xmlHttpRequestObj.onreadystatechange= function(){this_.actionOnStateChange();}
Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 08:34 PM.


Advertisement
Log in to turn off these ads.