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
반응형