Ajax simple, first trial not working...help?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Ajax simple, first trial not working...help?

Post by sarris »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is my first trial to use ajax.

[syntax="javascript"]
    function makeRequest(url,id_wanted) {

        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up  Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() { GetData(http_request,id_wanted); };
        http_request.open('GET', url, true);
        http_request.send(null);

    }

    function GetData(http_request,id_wanted) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                /*HERE I GET THE DATA USING THE DOM Wont put that code here to make it more readable*/
            	//RESPONSE
	}
            } else {
                alert('There was a problem with the request.');
            }
      }
 }
When i call the makeReques() function i get the alert "There was a problem with the requst"
I have checked it out and the request status, aparently and obviously, is 404, and not 200. The readystate is 4 = ok
Do you have any idea why can that be?
What should i check?


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

If the error is a 404 error, then perhaps the page cannot be found...
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

i call the function in that manner...is there anything wrong there?


<select size="1" name="Area" onchange = "javascript: makeRequest('www.houseonmap.gr/areadata.xml', this.value)">


This xml exists...you can confirm.[/u][/b]
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

If the xml file exists, then you're not getting a 404 error
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://www.houseonmap.gr/areadata.xml should be http://www.houseonmap.gr/areadata.xml
The request can fail or permission can be denied, both causing an exception, use

Code: Select all

try {
	http_request.onreadystatechange = function() { GetData(http_request,id_wanted); };
	http_request.open('GET', url, true);
	http_request.send(null); 
}
catch(e) {
	alert(e);
}
a readyState != 4 is not an error notification.
http://msdn2.microsoft.com/en-us/library/ms534361.aspx wrote:0 (Uninitialized) The object has been created, but not initialized (the open method has not been called).
1 (Open) The object has been created, but the send method has not been called.
2 (Sent) The send method has been called, but the status and headers are not yet available.
3 (Receiving) Some data has been received. Calling the responseBody and responseText properties at this state to obtain partial results will return an error, because status and response headers are not fully available.
4 (Loaded) All the data has been received, and the complete data is available.
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

yeah it was the http before the address thing!!!
thanks a lot!
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

things work but i have another related issue now that i'd like to ask.
When i call the html page and make the ruquest to the server i get connected (status 200) and all is ok.
However if i make another request it fails, so i have to close my browser in order to be able to make a request. Its sound reasonable, i guess i have to close the previous connection. Is there any way to do that?
Will .abort() do the thing?
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


well i thought things were ok but not really...this is what i do. you can check this test on [url]http://www.houseonmap.gr/test2.html[/url]
[syntax="html"]<html>
<head>
<script language="javascript"  type="text/javascript">


function getHTTPObject() {
	if (typeof XMLHttpRequest != 'undefined') {
		return new XMLHttpRequest();
	}
	try {
		return new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {} } return false;
}

function makeRequest(id_wanted){
	var url = "http://www.houseonmap.gr/areadata.xml";
	var http_request = getHTTPObject();
        http_request.open("GET", url, true);
        http_request.onreadystatechange = function() {
               document.form1.T5.value = "In here but ready state always undefined";
               if(http_request.readystate == 0) {document.form1.T1.value = "0";}
	       if(http_request.readystate == 1) {document.form1.T1.value = "1";}
               if(http_request.readystate == 2) {document.form1.T2.value = "2";}
               if(http_request.readystate == 3) {document.form1.T3.value = "3";}
               if(http_request.readystate == 4) {document.form1.T4.value = "4";}
	}
       http_request.send(null);
}



</script>
</head>

</body>
<form method="POST"  name = "form1">
	<p><input type="text" name="T1" size="40"></p>
	<p><input type="text" name="T2" size="40"></p>
<p><input type="text" name="T3" size="40"></p>
<p><input type="text" name="T4" size="40"></p>
<p><input type="text" name="T5" size="40"></p>
</form>
<p>


<select size="1" name="Area" onchange = "javascript: makeRequest(this.value)">
	<option value="1" selected>Selection1</option>
	<option value="2">Selection2</option>
	<option value="3">Selection3</option>

	</select></p>
</p>
</body>
</html>
It seems as the state of the http_request changes, as the fucntion is called, but the values is neither 0,1,2,3 or 4
Can you find out why is that?
thanks and merry christmas


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

readyState, attributes are case-sensitive.
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

thanks volka!!
Post Reply