Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX总是认为php返回是成功的,即使在失败之后也是如此_Javascript_Php_Ajax - Fatal编程技术网

Javascript AJAX总是认为php返回是成功的,即使在失败之后也是如此

Javascript AJAX总是认为php返回是成功的,即使在失败之后也是如此,javascript,php,ajax,Javascript,Php,Ajax,我有一个添加新用户帐户的php脚本。addAccount()的一部分检查用户名是否有效,如果无效,则返回一个不可用的异常(显示致命错误)。我的问题是AJAX将一切解释为成功,并显示成功消息,不管发生什么。如何修复此问题,或者至少捕获致命错误并显示正确的消息 $(document).on('click', '#createUserBtn', function(e){ e.preventDefault(); $.ajax({ url:'addUser.php',

我有一个添加新用户帐户的php脚本。addAccount()的一部分检查用户名是否有效,如果无效,则返回一个不可用的异常(显示致命错误)。我的问题是AJAX将一切解释为成功,并显示成功消息,不管发生什么。如何修复此问题,或者至少捕获致命错误并显示正确的消息

$(document).on('click', '#createUserBtn', function(e){
    e.preventDefault();
    $.ajax({
        url:'addUser.php',
        type:'post',
        data:$('#addUser').serialize(),
        success:function(){

                toastr.success("User successfully added!");
            },
        error: function(){
            toastr.warning('Uh-oh! Something went wrong with adding this user!');
        }
    });
});

addUser.php

<?php
session_start();
/* Include the database connection file (remember to change the connection parameters) */
require './db_inc.php';

/* Include the Account class file */
require './account_class.php';

  $type = $_POST['type'];
  $username = $_POST['uname'];
  $password = $_POST['password'];
  $comp = $_POST['company'];
  $email = $_POST['email'];
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $query = $pdo->query("SELECT * FROM accounts WHERE email ='".$email."'");

$account = new Account();
// Will print all the values received.
    $newId = $account->addAccount($username, $password, $comp, $email, $fname, $lname, $type);
    header('Location: ./dashboard.php?user='.$username);



?>

header('Location: ./dashboard.php?user='.$username);
您可以使用返回HTTP错误代码(通常为代码500:内部服务器错误)

您可以使用try/catch块执行此操作:

try
{
     ...
     $account = new Account();
     // Will print all the values received.
     $newId = $account->addAccount($username, $password, $comp, $email, $fname, $lname, $type);
     header('Location: ./dashboard.php?user='.$username);
}
catch(Exception $e)
{
    http_response_code(500);
    exit();
}

首先,如果您从javascript请求某些内容,则无法通过php重定向用户, 从addUser.php中删除此行

<?php
session_start();
/* Include the database connection file (remember to change the connection parameters) */
require './db_inc.php';

/* Include the Account class file */
require './account_class.php';

  $type = $_POST['type'];
  $username = $_POST['uname'];
  $password = $_POST['password'];
  $comp = $_POST['company'];
  $email = $_POST['email'];
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $query = $pdo->query("SELECT * FROM accounts WHERE email ='".$email."'");

$account = new Account();
// Will print all the values received.
    $newId = $account->addAccount($username, $password, $comp, $email, $fname, $lname, $type);
    header('Location: ./dashboard.php?user='.$username);



?>

header('Location: ./dashboard.php?user='.$username);
现在,要将结果从php返回到客户端,您必须使用值
DIE
php,最好的方法是JSON

在addUser.php中,检查所需内容并返回如下值:

    <?php

session_start();
/* Include the database connection file (remember to change the connection parameters) */
require './db_inc.php';

/* Include the Account class file */
require './account_class.php';

  $type = $_POST['type'];
  $username = $_POST['uname'];
  $password = $_POST['password'];
  $comp = $_POST['company'];
  $email = $_POST['email'];
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $query = $pdo->query("SELECT * FROM accounts WHERE email ='".$email."'");

$account = new Account();
// Will print all the values received.
    $newId = $account->addAccount($username, $password, $comp, $email, $fname, $lname, $type);
        if(intval($newId) > 0) // if user created
            die(json_encode(array('status' => 'ok')));
        else
            die(json_encode(array('status' => 'error')));
    ?>

您必须发送一个HTTP错误代码(从PHP脚本返回的任何内容都将自动响应代码200)。参数化您的查询。这不是PDO应该使用的方式。我以前从未这样做过。你能帮我起草一份吗?代码在哪里?你在哪里检查它是否存在?似乎缺少很多代码。如何进行php检查?addAccount()返回插入帐户的ID,因此我必须检查是否有错误或整数?@CodedUnknown尝试以下操作:
如果(intval($newId)>0)死亡(json_encode(数组('status'=>'ok'));else die(json_编码(数组('status'=>'error'))在“success:function(response){@CodedUnknown”上显示意外标识符。我更新了答案,请注意,
respone->status
更改为
response['status']
仍然没有加载。这一行出现错误
success:function(response){//