Clean way to throw php exception through jquery/ajax and json

前端 未结 5 743
孤城傲影
孤城傲影 2020-12-04 18:02

Is there a clean, easy way to throw php exceptions through a json response jquery/ajax call.

5条回答
  •  借酒劲吻你
    2020-12-04 18:46

    Facebook do something in their PHP SDK where they throw an exception if a HTTP request failed for whatever reason. You could take this approach, and just return the error and exception details if an exception is thrown:

     $results
        ));
    }
    catch (Exception $e) {
        echo json_encode(array(
            'error' => array(
                'code' => $e->getCode(),
                'message' => $e->getMessage()
            )
        ));
    }
    

    You can then just listen for the error key in your AJAX calls in JavaScript:

    
    

提交回复
热议问题