로메오의 블로그

[ajax - php] exception 처리 본문

Backend/PHP

[ajax - php] exception 처리

romeoh 2022. 2. 26. 23:38
반응형

Ajax

$.ajax({
    url: '/server/address',
    method: 'POST',
    //dataType: 'json',
    data: {
        text: ''
    }
})
.done(function(json) {
    console.log(json)
})
.fail(function(xhr, status, errorThrown) {
    console.log('fail', xhr, status, errorThrown)
})
.always(function(xhr, status) {
    // console.log('always', xhr, status)
})

 

PHP

<?php

$method = $_SERVER['REQUEST_METHOD'];
if ($method != 'POST') throw new Exception('', 1);

try {
	if ( empty($text) ) throw new Exception('empty data', 9000);	
    
} catch(Exception $e) {
	header('HTTP/1.1 500 Internal Server Error');
	echo json_encode(array(
        'error' => array(
            'msg' => $e->getMessage(),
            'code' => $e->getCode(),
        ),
    ));
    exit;
}

?>

 

 

Postman

반응형

'Backend > PHP' 카테고리의 다른 글

PHP 한글 깨짐 방지  (0) 2022.03.01
.PHP 확장자 제거하기  (0) 2021.08.30
PHP에서 긴 문자열 처리 [EOT]  (0) 2021.08.23
MAMP 설치하기 [Windows]  (0) 2021.04.27
PHP에서 Stored Procedure 호출하기  (0) 2020.12.09
Comments