如何使用带有倍数状态代码的Slimframework 3响应Jquery $.post调用?
我的代码是:
Javascript:
$.post('/insert/table/' + $pedido, table_data)
.done(function(data, statusText, xhr) {
alert(xhr.status);
if (xhr.status == 201) {
alert('Done');
} else {
alert('Error');
}
})
.fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
});PHP:
$this->post('/insert/table/{pedido}',
\App\Controller\Master\MasterController:SaveTable');
public function SaveTable($request, $response, $args) {
$params = (object) $request->getParams();
$logger = $this->container->get('logger');
$pedido = $args['pedido'];
if($pedido < 1){
$logger->error('Arg:Cod Pedido error.');
$response->withStatus(503);
return $response;
}
$conn = $this->container->get('DB_Producao');
if($conn) {
$tsql= "INSERT INTO .......";
if(!sqlsrv_query($conn, $tsql)) {
$logger->error('error');
$response->withStatus(503);
return $response;
die();
}
}
$response->withStatus(201)
->withHeader('Content-Type', 'text/plain')
->write('Done');;
return $response;
}使用此代码,我总是从post操作中接收状态代码200。我还以为会有201或503的状态码。
发布于 2021-02-21 10:38:51
请求和响应头是“不可变的”。
而不是这样:
$response->withStatus(503);
return $response;试试这个:
return $response->withStatus(503);或
$response = $response->withStatus(503);
return $response;// The headers are immutbale
$response = $response->withStatus(201)
->withHeader('Content-Type', 'text/plain');
// The body is not immutable
$response->getBody()->write('Done');
return $response;https://stackoverflow.com/questions/66298787
复制相似问题