Jquery 和 Slimframework - 状态请求
Jquery and Slimframework - Status Request
我如何使用 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 状态代码。
请求和响应headers是“不可变的”。
而不是这个:
$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;
我如何使用 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 状态代码。
请求和响应headers是“不可变的”。
而不是这个:
$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;