Angularjs 如何在Slim框架中设置自定义响应状态代码
我在新项目中使用Angular JS、PHP和Slimframework 我在Angular JS中使用承诺,但正如您所知,如果响应状态代码从API返回为200,则只有“成功”部分可以工作 在Slimframework中,默认响应状态代码为200,所有我的请求返回时状态代码为200。(如果没有错误,则为正常) 但我想做一些验证,如果验证失败,我只想在Slim框架中更改或设置自定义状态代码。在本例中,我将能够用“error”代码块捕获Angular JS中的问题 例如: 细长框架部件Angularjs 如何在Slim框架中设置自定义响应状态代码,angularjs,frameworks,slim,Angularjs,Frameworks,Slim,我在新项目中使用Angular JS、PHP和Slimframework 我在Angular JS中使用承诺,但正如您所知,如果响应状态代码从API返回为200,则只有“成功”部分可以工作 在Slimframework中,默认响应状态代码为200,所有我的请求返回时状态代码为200。(如果没有错误,则为正常) 但我想做一些验证,如果验证失败,我只想在Slim框架中更改或设置自定义状态代码。在本例中,我将能够用“error”代码块捕获Angular JS中的问题 例如: 细长框架部件 $app-&
$app->get('/getUserInformations/{user_id}', function(Request $request, Response $response){
if(isLoggedIn()){
//Do something and send an answer with status code 200
}else{
//Send some errors with status code 500 or something else
}
});
角JS部件:
user.getUserInformations = function(){
var defer = $q.defer();
$http.get($rootScope.defaultApiPath + '/getUserInformations/1')
.success(function(response){
//if status code is 200 this means user logged in. Do something
defer.resolve(response);
})
.error(function(error, status){
// if status code is 500 this means not logged in. Show error message
defer.reject(error);
});
return defer.promise;
};
在SlimFramework的文档中,我发现
$newResponse = $response->withStatus(302);
$data = array('name' => 'Rob', 'age' => 40);
$newResponse = $oldResponse->withJson($data, 201);
但是我不能用它们。给我一些找不到方法的错误
简而言之,我的问题是,如何在slim框架中设置自定义状态代码
提前谢谢你你想要的答案是
return $response->withStatus(xxx);
您要寻找的答案是
return $response->withStatus(xxx);
返回$response->with status(xxx);请你把它写下来作为回答好吗。我想把它设为正确答案:)非常感谢。50行问题,1行答案。很好..返回$response->with status(xxx);请你把它写下来作为回答好吗。我想把它设为正确答案:)非常感谢。50行问题,1行答案。伟大的