无法将数据从Angular / Ionic应用程序发布到Laravel API

时间:2016-02-20 03:06:46

标签: angularjs laravel post eloquent

我正在尝试将数据从角度(离子移动应用)发布到远程Laravel 5 REST API。这在使用Postman发布数据时有效,但是在我的生活中无法在尝试通过Angular前端时将其保存到数据库中。这是我的Angular代码以及用于保存数据的Laravel控制器。

控制器/ authentication.js

var app = angular.module('myApp.controllers.authentication', []);

app.controller('LoginCtrl', function ($scope, $state, $ionicLoading, AuthService) {
    $scope.loginInstagram = function() {
        AuthService.loginInstagram()
            .then(function() {
                console.log('Signed In Insta');
                $state.go("tab.home");
            });
    }
});

服务/ authentication.js

var app = angular.module('myApp.services.authentication', []);

app.service('AuthService', function ($q, $http, $cordovaOauth, $ionicPopup) {
     var self = {
          'loginInstagram': function() {
            var d = $q.defer();

            $cordovaOauth.instagram('42e2f60b94e1487db4617a48a7000453', [], {})
                .then(function(result) {
                     console.log("Response Object -> " + JSON.stringify(result));


                    // save the access token
                    var data = { instagram_access_token: result.access_token };

                          $http.post("http://localhost:8000/api/user/signupInstagram", data).success(function(data, status) {
                           console.log(data);
                          });   
                }, function(error) {
                    console.log("Error -> " + error);
                });

               return d.promise;

        }
     };

     return self;
});

应用程序/ HTTP /控制器/ UsersController.php

<?php

namespace App\Http\Controllers;

use Input;
use Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;
use App\User;

class UsersController extends Controller
{
    // Gets all users in the users table and returns them
    public function index()
    {
        $users = User::all();

        return $users;
    }

    public function signupInstagram() {
        $user = new User();

        $user->instagram_access_token = Input::get('instagram_access_token');

        $user->save();
    }
}

0 个答案:

没有答案