working with ajax
I am working with Ajax this is the first time I am learning about it. I have a few questions if anyone would be nice to explains. I am working on something for my class he gave us this to use
<html>
<head>
<title>Ajax Examples</title>
<style type="text/css">
body {
font-family: Trebuchet MS;
color: #173F5F;
background-color:#BFFFFF;
margin:10px;
}
.formtable{
border:2px solid #009999;
background-color:#006B6b;
color:#FF6600;
}
.btnSubmit{
color:#FF6600;
background-color:#BFFFFF;
border:2px inset #FF6600;
width:100px;
}
h1{
color:#FF6600;
}
input, textarea{
color:#FF6600;
border:1px solid #FF6600;
}
hr{
background-color:#FF6600;
height:3px;
}
#results{
color:#FF6600;
width:300px;
}
</style>
<script type="text/javascript">
function loadurl(dest) {
try {
// Moz supports XMLHttpRequest. IE uses ActiveX.
// browser detection is bad. object detection works for any browser
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
//browser doesn't support
alert("Get with the times man!");
}
// the xmlhttp object triggers an event everytime the status changes
// triggered() function handles the events
xmlhttp.onreadystatechange = triggered;
// open takes in the HTTP method and url.
xmlhttp.open("GET", dest);
// send the request. if this is开发者_StackOverflow a POST request we would have
// sent post variables: send("name=valerie&gender=female)
// Moz is fine with just send(); but
// IE expects a value here, hence we do send(null);
xmlhttp.send(null);
}
function triggered() {
// if the readyState code is 4 (Completed)
// and http status is 200 (OK) we go ahead and get the responseText
// other readyState codes:
// 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
// xmlhttp.responseText object contains the response.
document.getElementById("output").innerHTML = xmlhttp.responseText;
}
}
</script>
<title>LAB 14</title>
</head>
<body>
<h1>Simple Ajax</h1>
<hr>
<p>This page will automatically load another file into page below.</p>
<a href="#" onClick="loadurl('info.html')" >click here to load another file</a>
<div id="output"></div>
</body>
</html>
Now all he said is is for us to make your info.html page about you - you could make it a resume or something else of your choice. Now I have one that i did. My question would be I have a ftp server we are suppose to upload it to. Would i be able to see the page before its uploaded to see if it works or am I doing something or have something wrong on the page that is not correct.
Unless you are running a web server on your local machine, this won't work. You see this part?
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
That means that unless the JavaScript receives a "200 OK" status code from the server that is hosting the page, it won't ever process the part inside the if()
block.
You can try this instead:
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200 || (xmlhttp.status == 0 && document.location.protocol == "file:"))) {
No guarantee this will work, but it should.
Instead of editing your code, it might be easiest just to install a simple webserver to test your code on.
For super simple stupid, I would recommend AnalogX's SimpleServer if you are on windows. It is really lightweight and does what you need.
精彩评论