PHP对AJAX请求无响应 [英] No response from PHP on AJAX request

查看:114
本文介绍了PHP对AJAX请求无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery和PHP使用AJAX开发简单的注册页面.

I am working on a simple signup page using jQuery and PHP using AJAX.

以下是进行ajax调用的脚本:

Here is the script for making the ajax call:

<script>
    function submitForm(){
      var data1=$('#regform').serialize();
      $.ajax({
        type:'POST',
        url:'signup.php',
        data:data1,
        success: function(response){
          console.log(response);
          if(response==1)
            alert('taken');
            else if(response==2)
              alert('registered');
              else
                alert(response);
        }
      });
    }
  </script>

以及响应该调用的PHP脚本:

and the PHP script which responds to the call:

signup.php:

signup.php:

<?php
require_once 'dbconnect.php';
if($_POST) {
    $user_name = $_POST['user_name'];
    $user_email = $_POST['user_email'];
    $user_password = $_POST['password'];  
    try {
        $sth = $dbh->prepare("SELECT * FROM logindata WHERE email=:email");
        $sth->execute(array(":email"=>$user_email));
        $count = $sth->rowCount();   
        if($count==0){    
            $sth = $dbh->prepare("INSERT INTO logindata(username,email,pass) VALUES(:uname, :email, :pass)");
            $sth->bindParam(":uname",$user_name);
            $sth->bindParam(":email",$user_email);
            $sth->bindParam(":pass",$user_password);   
            if(!$sth->execute()) {
                echo "3";  
            } else {
                echo "2";
            }
        } else{    
        echo "1"; 
        }    
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
}
?>

使用PDO.

dbconnect.php:

dbconnect.php:

<?php
  $dbhost='localhost';
  $dbuser='root';
  $dbpass='';
  $dbname='ambitio';  
  try{
    $dbh=new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  }
  catch(PDOException $e){
    echo $e->getMessage();
  }  
?>

问题: 这两个响应都没有返回到jQuery ajax调用.我用console.log()进行了检查,但浏览器中未显示任何内容.数据已存储在MySQL数据库中(选中该数据库),这意味着execute()在PHP中可以正常工作,但浏览器中仍未显示任何警报.如果count评估为0,则1确实会返回.

Problem: Neither of the two responses are being returned to the jQuery ajax call. I checked with console.log() but nothing shows up in the browser. Data gets stored in the MySQL database (checked it) which means that execute() is working fine in PHP, but still no alert is shown in the browser. 1 does get returned in case count evaluates to 0.

此外,当我刷新表单而不实际提交表单时,在浏览器控制台上出现错误POST 412 (Precondition Failed).

Also, when I refresh the form without actually submitting the form, I get the error POST 412 (Precondition Failed) at the browser console.

发布了完整的php.

Apache访问日志

Apache access log

::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/css/bootstrap.css HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/css/overboot.css HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/css/font-awesome.css HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/js/jquery.js HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/js/bootstrap.js HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [20/Jul/2016:00:43:36 +0530] "GET /ambitio/signup.html HTTP/1.1" 200 1454 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [20/Jul/2016:00:43:46 +0530] "POST /ambitio/signup.html HTTP/1.1" 200 1454 "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [20/Jul/2016:00:43:46 +0530] "POST /ambitio/signup.php HTTP/1.1" 200 1 "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"

表单HTML标记:

<form method="post" id="regform" onSubmit="submitForm()">
      <input type="text" placeholder="Username" id="user_name" name="user_name" />
      <input type="email" placeholder="Email" id="user_email" name="user_email" />
      <input type="password" placeholder="Password" id="password" name="password" />
      <input type="password" placeholder="Retype password" id="rpassword" name="rpassword" />
      <input type="submit" id="submit"/>
</form>

推荐答案

由于您的表单不包含要发送到的网址,并且您的onSubmit -Handler(submitForm)不返回false,因此表单实际上会被浏览器发布到当前所在的页面上(这很可能无法处理表单的POST数据),并且只会重新加载包含登录表单的页面.

Since your form doesn't contain a url to send to and your onSubmit-Handler (submitForm) doesn't return false, the form will actually get posted by the browser to the page it's currently on (which is most likely not capable of processing the form's POST data) and will just reload the login-form containing page.

该POST也可能会发送到您的Ajax脚本 ,但是您的浏览器不会等待响应,因为它已经继续运行.

The POST will likely be send to your ajax script as well, but your browser won't wait for the reponse, because it already moved on.

请参见 https://html.spec.whatwg.org/multipage/forms.html#concept-form-submit 获取有关提交过程的更多详细信息.

See https://html.spec.whatwg.org/multipage/forms.html#concept-form-submit for further details on the submit process.

解决方案是将return false添加到submitForm函数或onSubmit处理程序(;return false)

Solution would be to either add return false to your submitForm function or to the onSubmit handler (;return false)

这篇关于PHP对AJAX请求无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆