返回JSON对象形式的Laravel 5.7 API时,AngularJS中的$ http错误数据错误

时间:2019-02-08 06:37:46

标签: angularjs json laravel api postman

在集成Laravel API时,在AngularJS中获取错误数据错误。邮递员的回复工作正常。失败响应工作正常,但是成功响应始终报告为不良数据。

尝试修改请求的标题数据部分:

//Angular code snippet

var app = angular.module('adminLogin', []);

    app.controller('adminLoginController', ['$scope', '$http', function($scope, $http) {

        $scope.checkLogin = function(){    
            $http({
                method : "POST",
                url : "http://ri/api/adminLogin",
                headers: { 'Content-Type' :'application/json'},
                data : {
                    admin_id : $scope.admin_id,
                    password : $scope.pwd,
                    device_id : 'dummy_id'
                }
            })
            .then(function(response) {
                console.log(response);
                if(response.data.type=="success")
                {
                    alert("success");
                    console.log(response);
                }
                else
                {
                    $scope.errorMsg = response.data.message;
                    console.log(response);
                }

            }, function(response) {
                console.log(response);
                $scope.errorMsg = "Unexpected Error Occured!!";
            });
        }
    }]);

Laravel API 控制器模型部分:

public function login(Request $request) //controller
{
    $result = Admin::paginate();

    $admin = new Admin;
    return $admin->newLogin($request,$result->total());   
}

public function newLogin($request, $count) //model
{
    $result = DB::table('admin_details')->where('admin_id', $request->admin_id)->first();

    if($result != NULL)
    {
        if($request->password == $result->password)
        {
            if($count==1)
            {
                $this->removeActiveLogin($request->admin_id, $request->device_id);
            }

                $return_value = DB::table('admin_login_details')->insert([
                    'admin_id' => $request->admin_id, 
                    'timezone' => $result->timezone,
                    'device_id' => $request->device_id 
                ]);

                if($return_value != NULL)
                {
                    $request->session()->flush();
                    $request->session()->regenerate();
                    $session_id = $request->session()->getId();
                }

                $return_value2 = DB::table('admin_session_details')->insert([
                    'admin_id' => $request->admin_id, 
                    'session_id' => $session_id
                ]);

                if($return_value!=NULL && $return_value2!=NULL) 
                {
                    return response()->json([
                        'type' => 'success',
                        'session_id' => $session_id,
                        'device_id' => $request->device_id,
                        'message' => 'Login successful'
                    ]);
                }
                else
                {
                    return [
                        'type' => 'failure',
                        'message' => 'Unable to login'
                    ];
                }
        }
        else
        {
            return [
                'type' => 'failure',
                'message' => 'Incorrect Password'
            ];
        } 
    }
    else
    {
        return [
            'type' => 'failure',
            'message' => 'Incorrect User ID'
        ];
    }

}

与Postman一起测试时,API工作正常。但是,当与AngularJS集成时,“成功”部分将被报告为不良数据。

  

错误:“ [$ http:baddata] http://errors.angularjs.org/1.6.9/ $ http / baddata?p0 = 1%7B%22type%22%3A%22success%22%2C%22session_id%22%3A%22Q1FDSjzGvEIIZJ8JeUrUphhXGNaJsDpqiO8VqeJA%22 2C%22device_id%22%3A%22dummy_id%22%2C%22message%22%3A%22Login%20successful%22%7D&p1 =%7B%7D“

0 个答案:

没有答案