数据通过REST api中的控制器传递

时间:2016-07-27 05:12:37

标签: javascript angularjs ionic-framework

我是Ionic和angularjs的新手。我正在尝试从signin.html页面获取登录(ng-model)数据(电子邮件和密码),并使用POST方法调用API。但它不会调用api。我花了几天时间。请帮帮我!

HTML

  <ion-view view-title="login">
    <ion-component>
    </br>

    <div class="list list-inset">
    <label class="item item-input">
    <input type="text" placeholder="First Name" ng-model="login.firstname">
    </label>
    <label class="item item-input">
    <input type="password" placeholder="Password" ng-model="login.password">
    </label>

    <label class="item item-input">
    <input type="text" placeholder="E-mail" ng-model="login.email">
    </label>

    <button class="button button-block button-balanced" data-ng-click="submit(login)">
    Login
    </button>
    <button class="button icon-left button-block ion-social-facebook button-positive">Login Facebook</button>

    <button class="button icon-left button-block ion-social-googleplus button-assertive">Login Google+</button>

    <a ng-href="#/signup"><button class="button button-clear button-positive">SignUp
    </button></a>
    </div>

    </ion-component>
    </ion-view>

app.js

  var app=angular.module('starter', ['ionic','ngCordova'])
    app.controller('loginCtrl', function($scope,$http){
    $scope.data=[];
    // $scope.logindata = {};
    // console.log (login);
    $scope.login={};
    //****login submit****
    $scope.submit=function(login){
    console.log(login);
    $scope.logincontet=console.log('logindata:'+login);
    //***api call***

    var local1='json={"method_name":"login","body":{"login_with":"2","facebook_id":"","email":';
    var local2=',"name":"",';
    var local3='"password":';
    var local4='}}';
    var req =
    {
    method: 'POST',
    url: "http://192.168.1.23/projects/veterinary/webservice/main.php",
    //data: 'json={"method_name":"login","body":{"login_with":"2","facebook_id":"","email":"$scope.login.email","name":"","password":"$scope.login.password"}}',
    data:local1 + $scope.login.email + local2 + local3 + $scope.login.password +local4,
    headers: {"Content-Type": "application/x-www-form-urlencoded"}
    };

    $http(req).success(function(data, status, headers, config)
    {
    console.log('Success',data);
    $scope.data=data.data;
    //success
    })
    .error(function(data, status, headers, config)
    {
    console.log('Error');
    });//error
    //*********
    window.location.href = "#/success.html";
    }
    //*****
    //****login api call****


    });

2 个答案:

答案 0 :(得分:0)

app.config(function($stateProvider, $urlRouterProvider){
$stateProvider

.state('login', {
url: "/login",
templateUrl: "templates/login.html",
controller: 'loginCtrl'
})

.state('signup', {
url: "/signup",
templateUrl: "templates/signup.html",
controller: 'signupCtrl'
})



// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/login');

});
*********************
Binding view and controller!

答案 1 :(得分:0)

最后我用这个创建了post api字符串......      data:'json = {“method_name”:“login”,“body”:{“login_with”:“2”,“facebook_id”:“”,“email”:“'+ $ scope.login.email +'”, “名称”: “”, “密码”: “ '+ $ scope.login.password +'”}}”,