Javascript 使用ajax

Javascript 使用ajax,javascript,ajax,Javascript,Ajax,我正在使用Ajax,这是我第一次了解它。如果有人愿意解释,我有几个问题。我正在为我的班级做一些事情,他给了我们这个 <html> <head> <title>Ajax Examples</title> <style type="text/css"> body { font-family: Trebuchet MS; color: #173F5F; background-color:#BFFFFF;

我正在使用Ajax,这是我第一次了解它。如果有人愿意解释,我有几个问题。我正在为我的班级做一些事情,他给了我们这个

<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 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>

Ajax示例
身体{
字体系列:投石机MS;
颜色:#173F5F;
背景色:#bffff;
利润率:10px;
}
.表格{
边框:2px实心#009999;
背景色:#006B6b;
颜色:#FF6600;
}
.btnSubmit{
颜色:#FF6600;
背景色:#bffff;
边框:2张插图#FF6600;
宽度:100px;
}
h1{
颜色:#FF6600;
}
输入,文本区{
颜色:#FF6600;
边框:1px实心#FF6600;
}
人力资源{
背景色:#FF6600;
高度:3倍;
}
#结果{
颜色:#FF6600;
宽度:300px;
}
函数loadurl(dest){
试一试{
//Moz支持XMLHttpRequest.IE使用ActiveX。
//浏览器检测不正确。对象检测适用于任何浏览器
xmlhttp=window.XMLHttpRequest?新的XMLHttpRequest():
新的ActiveXObject(“Microsoft.XMLHTTP”);
}
第(e)款{
//浏览器不支持
警惕(“跟时代人打交道!”);
}
//每当状态更改时,xmlhttp对象都会触发一个事件
//函数的作用是:处理事件
xmlhttp.onreadystatechange=已触发;
//open接受HTTP方法和url。
open(“GET”,dest);
//发送请求。如果这是POST请求,我们将
//发送后变量:发送(“姓名=瓦莱丽,性别=女性”)
//Moz可以只发送();但是
//IE在这里需要一个值,因此我们发送(null);
xmlhttp.send(空);
}
函数触发(){
//如果readyState代码为4(已完成)
//http状态是200(OK),我们继续并获得responseText
//其他readyState代码:
//0=未初始化1=加载2=加载3=交互式
if((xmlhttp.readyState==4)和&(xmlhttp.status==200)){
//xmlhttp.responseText对象包含响应。
document.getElementById(“输出”).innerHTML=xmlhttp.responseText;
}
}
实验14
简单Ajax

此页面将自动将另一个文件加载到下面的页面中


现在他所说的就是让我们制作关于你的info.html页面-你可以把它做成简历或其他你选择的东西。现在我有一个我做过的。我的问题是我有一个ftp服务器,我们应该把它上传到。我能不能在上传之前看到这个页面,看看它是否工作,或者我正在做什么,或者有什么工作要做ng在页面上显示不正确。

除非您在本地计算机上运行web服务器,否则此操作将不起作用。看到此部分了吗

if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
这意味着,除非JavaScript从承载页面的服务器接收到状态代码,否则它永远不会处理
if()
块中的部分

您可以尝试以下方法:

if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200 || (xmlhttp.status == 0 && document.location.protocol == "file:"))) {

不能保证这会起作用,但它应该起作用。

与其编辑代码,不如安装一个简单的Web服务器来测试代码


对于超级简单的傻瓜,如果你在windows上,我建议使用AnalogX。它非常轻量级,可以满足你的需要。

因为当我调出文件时,它不会单击链接来显示我键入的内容。好的,我理解,所以一旦我将其上传到我的filzila,我应该能够看到它。我只是在登录学校ftp时遇到了问题..