$ .ajax错误无法从php捕获抛出的异常 [英] $.ajax error cannot catch thrown exception from php

查看:136
本文介绍了$ .ajax错误无法从php捕获抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$.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代码我' m工作。以下是被调用的add_vendor.php文件。

This is the jquery code I'm working on. And the following is the add_vendor.php file being called.

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

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

Ok, That 'if($DB->insertDataToTable('mfVendor', $vendor_info_json))' part always is false to test $.ajax's error handling code. No matter what I do, error: callback is not working even if the php code throws an exception all the time.

我在这里遇到什么问题? :(

What are the problems I'm missing here? :(

推荐答案

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

When your server throws an Internal Server Error, from Javascript side it's still a success. In your success function you have to check the response's status code, and handle it accordingly.

这篇关于$ .ajax错误无法从php捕获抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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