使用ajax show打开新窗口

时间:2016-06-24 10:14:13

标签: javascript html ajax

在这段代码中有一些与链接相关的错误,而且它没有打开页面。在我给出的链接中可能存在错误,并且它没有显示其将拥有其他数据的警报page。在我的电脑上,文件的链接是" D:\ xampp \ htdocs \ cart \ welcome.php"表示它在文件夹中" D:\ xampp \ htdocs \ cart"现在我改变了什么使它正确,我在我的电脑上使用xampp而不是制作webstie



 <html>
<head>
<script>  
  function check(form) {
  if (form.email.value == "id@gmail.com") {
    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);
		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=id@gmail.com   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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

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

本地文件路径的URL将以file:而不是http:开头,但XMLHttpRequest的大多数实现都会拒绝支持它,并且它不会导致PHP被执行(因为PHP在这种情况下是服务器端编程语言。)