$.ajax({
    type: "POST",
    url: "functions/add_vendor.php",
    data:
    {
        vendor_info_json : vendor_info_json
    },
    success:function(result)
    {
        alert('Successfully Done.')
        location.reload();
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert('Error Message: '+ textStatus);
        alert('HTTP Error: '+ errorThrown);
    }
})


这是我正在使用的jQuery代码。以下是被调用的add_vendor.php文件。

<?php
include_once('../models/VENDOR_DB_MANAGER.php');

$vendor_info_json = $_REQUEST["vendor_info_json"];
$DB = new VENDOR_DB_MANAGER();
$DB->connectTo('BBY');
try
{
            //This ALWAYS returns false to test the code.
    if($DB->insertDataToTable('mfVendor', $vendor_info_json))
    {
        $DB->disconnect();

        header('Content-Type: application/json');
        $data = json_encode($result);
        echo $data;
    }
    else
    {
        throw new Exception("Error has been detected.");

    }
}
catch (Exception $e)
{
    header("HTTP/1.1 500 Internal Server Error");
            echo "Exception occurred: " . $e->getMessage();
}
?>


好的,如果测试$ .ajax的错误处理代码,则'if($ DB-> insertDataToTable('mfVendor',$ vendor_info_json))'部分始终为false。无论我做什么,都会出错:错误:即使php代码始终都抛出异常,回调也不起作用。

我在这里缺少什么问题? :(

最佳答案

当您的服务器引发内部服务器错误时,从Javascript方面来看,它仍然是成功的。在您的success函数中,您必须检查响应的状态码,并进行相应的处理。

关于php - $ .ajax错误无法捕获php引发的异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16472116/

10-09 05:29
查看更多