简体   繁体   English

Javascript XMLHttpRequest 和 Jquery $.ajax 都返回当前页面的 HTML 代码

[英]Javascript XMLHttpRequest and Jquery $.ajax both are returning the current page HTML code

The problem问题

Hello i want to start off hoping all of y'all are having a fantastic day!你好,我想开始希望你们所有人都度过了美好的一天! I'm having a really weird problem that i have never came across before.我遇到了一个我以前从未遇到过的非常奇怪的问题。 I have a bootstrap navigation form with two fields, an input for a email and a field for the password.我有一个带有两个字段的引导导航表单,一个是电子邮件输入,一个是密码字段。

带有表单的 Bootstrap 导航栏的图片

When the user submits the form it calls to an AddEventListener which is waiting for a click of the login button.当用户提交表单时,它调用一个 AddEventListener 等待登录按钮的点击。 and after that is called it validates(Server Side Validation) the form of the entered data.然后调用它验证(服务器端验证)输入数据的形式。 After both the email_validated and password_validated is both equal to true it calls to a function called checkLogin(email, password){email = entered email in field, password = entered password in field}.在 email_validated 和 password_validated 都等于 true 后,它调用一个名为 checkLogin(email, password){email = 在字段中输入的电子邮件,密码 = 在字段中输入的密码} 的函数。 In the checkLogin function it called to a JQuery $.ajax({}) call which sends the email and password field to a php document located in /scripts/checkLogin.php.在 checkLogin 函数中,它调用 JQuery $.ajax({}) 调用,该调用将电子邮件和密码字段发送到位于 /scripts/checkLogin.php 的 php 文档。 However instead of it returning "Hello"(For testing purposes) is returns the entire HTML code for the current page(As shown above)然而,它不是返回“Hello”(出于测试目的)而是返回当前页面的整个 HTML 代码(如上所示)

Ajax 请求响应

JQuery $.ajax({}) code JQuery $.ajax({}) 代码

Here i have the code for the $.ajax({}) call.这里我有 $.ajax({}) 调用的代码。

整个 $.ajax({}) 调用

Console安慰

I'm not getting any errors in the console except for the XHR finished loading: POST除了 XHR 完成加载之外,我在控制台中没有收到任何错误:POST

安慰

Other Pictures其他图片

Here are some other pictures that i am including to hopefully give yall a better idea of the structure.这里有一些其他的图片,我希望让你们更好地了解结构。

文件结构

checkLogin.php 代码

Note笔记

I just want to add that i have checked Stackoverflow for similar problems and other people have had this problem but they solutions did not work on my code.我只想补充一点,我已经检查过 Stackoverflow 是否存在类似问题,其他人也遇到过这个问题,但他们的解决方案对我的代码不起作用。 I have also tried just using a regular XMLHttpRequest and the same problem occurred, i'm honestly not sure what is wrong with the code for i have never had this problem before.我也尝试过只使用常规的 XMLHttpRequest 并且发生了同样的问题,老实说,我不确定代码有什么问题,因为我以前从未遇到过这个问题。 Thank you for taking the time to read this i appreciate any help that i can get with solving this.感谢您花时间阅读本文,感谢您在解决此问题时能获得的任何帮助。 I'm not the best with $.ajax({}) or XMLHttpRequest and im willing to make any changed to the code to try to get this to work.我不是最好的 $.ajax({}) 或 XMLHttpRequest,我愿意对代码进行任何更改以尝试使其正常工作。 Below is my code for the entire javascript validation(server side).下面是我整个 javascript 验证(服务器端)的代码。

Code代码

      <form class="form-inline my-2 my-lg-0" method="POST">

        <input class="form-control mr-sm-2 nav-login" type="text" autocomplete="off" placeholder="Email" name="email" value="trest@gmail.com" id="email">

        <input type="password" name="password" autocomplete="off" class="form-control nav-login mr-sm-2" placeholder="Password" value="password" name="password" id="password">

        <button class="btn btn-success my-2 my-sm-0 nr" type="button" name="login_button" id="login_button">Login &nbsp;<i class="fas fa-sign-in-alt"></i></button>

      </form>


      <script>
      document.getElementById('login_button').addEventListener('click', (e) => {
      e.preventDefault();

      // const email_regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

      let email_validated = true;
      let password_validated = true;



      let email = document.getElementById('email');
      let password = document.getElementById('password');

      // let email_length = email.value.length;
      // let password_length = password.value.length;




      //
      //
      // if(!email_length){
      //   email.classList.add('is-invalid');
      // }else{
      //   if(!email_regex.test(email.value)){
      //     email.classList.add('is-invalid');
      //   }else{
      //     email.classList.remove('is-invalid');
      //     email.classList.add('is-valid');
      //     email_validated = true;
      //
      //   }
      // }
      // if(!password_length){
      //   password.classList.add('is-invalid');
      // }else{
      //   password.classList.remove('is-invalid');
      //   password.classList.add('is-valid');
      //   password_validated = true;
      // }


      if(email_validated === true && password_validated === true){
        checkLogin(email.value, password.value);
      }
    });
    function checkLogin(email, password){
      $.ajax({
        type: "POST",
        url: '/scripts/checkLogin.php',
        data: {
          'email': email,
          'password': password
        },
        contentType: "application/json; charset=utf-8",
        success: (result) => {
          alert(result);
        }
      });
    }
</script>

Links to the other StackOverFlow question其他 StackOverFlow 问题的链接

ajax returns the html code of current page instead of json ajax返回当前页面的html代码而不是json

正如 t.niese 所说,我是用 xammp 配置我的服务器的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM