第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

AJAX總是認(rèn)為php返回成功,即使失敗

AJAX總是認(rèn)為php返回成功,即使失敗

PHP
GCT1015 2022-10-28 09:37:21
我有一個(gè)添加新用戶帳戶的 php 腳本。addAccount() 的一部分檢查用戶名是否有效,如果無效,則返回不可用的異常(出現(xiàn)致命錯(cuò)誤)。我的問題是 AJAX 將一切都解釋為成功并無論如何都顯示成功消息。如何解決此問題或至少捕獲致命錯(cuò)誤并顯示正確的消息?$(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!');        }    });});添加用戶.php<?phpsession_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);?>這是使用的 addAccount 函數(shù)...    public function addAccount(string $name, string $passwd, string $comp, string $email, string $fname, string $lname, string $type): int    {        /* Global $pdo object */        global $pdo;        /* Trim the strings to remove extra spaces */        $name = trim($name);        $passwd = trim($passwd);        /* Check if the user name is valid. If not, throw an exception */        if (!$this->isNameValid($name))        {            throw new Exception('Invalid user name');        }        /* Check if the password is valid. If not, throw an exception */        if (!$this->isPasswdValid($passwd))        {            throw new Exception('Invalid password');        }
查看完整描述

2 回答

?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

首先,如果您從 javascript 請求某些內(nèi)容,則無法通過 php 重定向用戶,請從 addUser.php 中刪除此行


header('Location: ./dashboard.php?user='.$username);

現(xiàn)在要將結(jié)果從 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')));

    ?>

然后更改您的客戶端,如下所示:


$(document).on('click', '#createUserBtn', function(e){

    e.preventDefault();

    $.ajax({

        url:'addUser.php',

        type:'post',

        data:$('#addUser').serialize(),

        dataType: "json", // <- Add this line

        success:function(response){ // <- add response parameter

                //Here check result

                if(response['status'] == 'ok')

                    toastr.success("User successfully added!");

                else

                    toastr.warning('Uh-oh! Something went wrong with adding this user!');


            },

        error: function(){


        },

    statusCode: { // <- Add this property

        500: function() {

             toastr.warning('Uh-oh! Something went wrong with adding this user!');

        }

    }

    });

});


查看完整回答
反對 回復(fù) 2022-10-28
?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以使用http_response_code()返回 HTTP 錯(cuò)誤代碼(通常代碼 500 :內(nèi)部服務(wù)器錯(cuò)誤)。

您可以使用 try/catch 塊來做到這一點(diǎn):

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();

}


查看完整回答
反對 回復(fù) 2022-10-28
  • 2 回答
  • 0 關(guān)注
  • 110 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號