AngularJS发布SLIM PHP API 400错误

时间:2015-01-21 04:11:40

标签: php angularjs api slim

我有一个angularjs控制器(帖子)


appControllers.controller('LoginController', ['$scope', '$location', 'httpG', function ($scope, $location, httpG, supersonic) {
 $scope.user = {};

 $scope.doLogIn = function () {

    httpG.post('login',{email: $scope.user.username, password: $scope.user.password}).success(function (data) {
        if (data.status) {
            httpG.setToken(data.info.token);
            $scope.isAuthenticated = true;
            $location.path('home');
        } else {
            alert("login error");
        }
    }).error(function (error) {
        alert("Login E");
    });
};

$scope.doLogOut = function () {
    httpG.removeToken();
};
}]);

使用另一个js文件中的函数:

post: function (uri, params) {
        params = params || {};
        return $http.post(serviceHost + uri, params);
    }

其中serviceHost是我的网站,uri是登录,params是从angularjs文件上面发送的。

到我的Slim PHP API:

$app->post('/login', function() use ($app) {
        // check for required params
        verifyRequiredParams(array('email', 'password'));

        // reading post params
        $email = $app->request()->post('email');
        $password = $app->request()->post('password');
        $response = array();

        $db = new DbHandler();
        // check for correct email and password
        if ($db->checkLogin($email, $password)) {
            // get the user by email
            $user = $db->getUserByEmail($email);

            if ($user != NULL) {
                $response["error"] = false;
                $response['name'] = $user['name'];
                $response['email'] = $user['email'];
                $response['apiKey'] = $user['api_key'];
                $response['createdAt'] = $user['created_at'];
            } else {
                // unknown error occurred
                $response['error'] = true;
                $response['message'] = "An error occurred. Please try again";
            }
        } else {
            // user credentials are wrong
            $response['error'] = true;
            $response['message'] = 'Login failed. Incorrect credentials';
        }

        echoResponse(200, $response);
    });

验证:

function verifyRequiredParams($required_fields) {
$error = false;
$error_fields = "";
$request_params = array();
$request_params = $_REQUEST;
// Handling PUT request params
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
    $app = \Slim\Slim::getInstance();
    parse_str($app->request()->getBody(), $request_params);
}
foreach ($required_fields as $field) {
    if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
        $error = true;
        $error_fields .= $field . ', ';
    }
}

if ($error) {
    // Required field(s) are missing or empty
    // echo error json and stop the app
    $response = array();
    $app = \Slim\Slim::getInstance();
    $response["error"] = true;
    $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
    echoResponse(400, $response);
    $app->stop();
}
}

我使用Chrome的高级REST客户端对API进行了测试,效果非常好。 我正在使用几本书,我认为我的发送有问题?我在apache日志文件中不断收到此错误:

“POST / api / v1 / login HTTP / 1.1”400 432

任何帮助将不胜感激 - 谢谢!

0 个答案:

没有答案