ajax callServer ajp仍然是空的

时间:2015-01-28 02:29:18

标签: javascript ajax

我正在尝试为一个项目创建一个网站,因此它将在本地,我正处于我想要使用Ajax作为我制作的登录表单的地步。我不太了解ajax,但这是一个代码,我发现它不起作用,但我不知道我还应该做什么,以及我到底想要什么。一个ajp文件里面有什么?提前致谢 这是javascript代码:

function callServer(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var xmlString = 
                        "<profile>" + 
                        "<username>" + escape(username) +"</username>" + 
                        "<password>" + escape(password) +"</password>" +                            
                        "</profile>";
// Build the URL to connect to
var url = "file:///C:/Users/admin/Documents/project/savelogin.jsp";
// Open a connection to the server
xmlHttp.open("POST", url, true);
// Tell the server you're sending it XML 
 xmlHttp.setRequestHeader("Content-Type", "text/xml");
 // Set up a function for the server to run when it's done
 xmlHttp.onreadystatechange = confirmUpdate;
// Send the request
xmlHttp.send(xmlString);
 }

这是html代码:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="login.js"></script>
<title>Project-Login</title>
</head>
<body>
<form id="loginform">
    <div>
    <p id="insertinfo">Type your info to log in your account:</p>
    <label for="Username">Username:</label>
    <input type="text" id="username" placeholder="Type your username"/>
    </div>
     <div>
    <label for="password">Type password:</label>
    <input type="text" id="password" placeholder="Type your password"/>
    </div>
    <div class="loginButton">
    <input type="submit" value="Log in" class="loginbutton" onclick="callServer()" />
    </div>
</form> 
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您将如何处理从HTML检索的值?

她是w3schools的一个例子:

http://www.w3schools.com/ajax/ajax_php.asp

但你想要的是:

  <script>
 function retrieveValues() {

var value1 = document.getElementById("username").value;

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("txtHint").innerHTML =   xmlhttp.responseText;
    }
}
xmlhttp.open("GET", "thePhpPage.php?q=" + value1, true);
xmlhttp.send();

}

但是这里的变量传递给了php函数。如果您想在本地机器上使用类似的东西,则需要根据您的操作系统安装灯泡或工具。