2016-06-24 84 views
-1

在此代码中有一些与链接相关的错误,它不打开页面。错误可能在我给的链接,它不显示警报在我的电脑中,文件的链接是“D:\ xampp \ htdocs \ cart \ welcome.php”,意思是它在文件夹“D:\ xampp \ htdocs \ cart”中,现在我改变了作出正确的,我用这个在我的电脑用XAMPP不做webstie使用ajax显示打开新窗口

<html> 
 
<head> 
 
<script> 
 
    function check(form) { 
 
    if (form.email.value == "[email protected]") { 
 
    document.getElementById("displayu").innerHTML ="correct username".fontcolor("green"); 
 
    } else if (form.email.value == "") { 
 
    document.getElementById("displayu").innerHTML ="<font color='red'>blank username</font>"; 
 
    } else if (form.email.value != "") { 
 
    var email = document.getElementById('email'); 
 
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
 

 
    if (!filter.test(email.value)) { 
 
     document.getElementById("displayu").innerHTML ="Please provide a valid email address"; 
 

 
    } 
 
     else {document.getElementById("displayu").innerHTML ="wrong email";} 
 
} 
 

 
    if (form.pswrd.value == "123") { 
 
    document.getElementById("displayp").innerHTML ="correct password"; 
 
    } else if (form.pswrd.value == "") { 
 
    document.getElementById("displayp").innerHTML ="<span style='color:red'>blank password</span>"; 
 
    } 
 
else {document.getElementById("displayp").innerHTML ="wrong password";} 
 
    makeRequest('http://D:/xampp/htdocs/cart/welcome.php'); 
 
    } 
 

 

 
    function makeRequest(url) { 
 
    httpRequest = new XMLHttpRequest(); 
 

 
    if (!httpRequest) { 
 
     alert('Giving up :(Cannot create an XMLHTTP instance'); 
 
     return false; 
 
    } 
 
    httpRequest.onreadystatechange = alertContents; 
 
    httpRequest.open('GET', url); 
 
    httpRequest.send(); 
 
    } 
 

 
    function alertContents() { 
 
    if (httpRequest.readyState === XMLHttpRequest.DONE) { 
 
     if (httpRequest.status === 200) { 
 
     alert(httpRequest.responseText); 
 
\t \t document.open(); 
 
     document.write(httpRequest.responseText); 
 
     document.close(); 
 
     
 
     } else { 
 
     alert('There was a problem with the request.'); 
 
     } 
 
    } 
 
    } 
 
</script> 
 
</head> 
 
<body> 
 
<h1>for login</h1> 
 
<h3>[email protected] and password=123</h3> 
 
<form name="login"> 
 
<div id="displayu"></div> 
 
<div id="displayp"></div> 
 
    Username <input type="text" name="email" id="email" /> 
 
    Password <input type="password" name="pswrd"/> 
 
    <input type="button" onclick="check(this.form)" value="Login" /> 
 

 
    <input type="reset" value="reset"/> 
 
</form> 
 

 
</body> 
 
</html>

+0

'http:// D:/ xampp/htdocs/cart/welcome.php'不是有效的URL。 Web服务器和文件系统是两个完全不同的东西。 – David

+0

''http:// D:/ xampp/htdocs/cart/welcome.php''把它改成''file:/// D:/ xampp/htdocs/cart/welcome.php'' ..但是为什么你需要提供“完整路径”或“绝对路径”,最好提供“相对链接”。 –

+0

为它创建一个提琴手,因此其他人很容易追踪你的bug。 –

回答

2

你需要运行一个Web服务器,然后请你的资源使用网络名称(例如http://localhost/cart/welcome.php)。

的URL到本地文件路径将与file:代替http:开始,但XMLHttpRequest的大多数实现会拒绝支持它,它不会导致被执行的PHP(自PHP,在这种情况下,是服务器侧编程语言)。

+0

我开始,但仍然显示错误 –

+0

而错误消息说...? – Quentin

+0

“请求存在问题。”基本上它在代码中作为警报 –