无法在angular2中验证用户身份

时间:2016-06-29 12:38:36

标签: javascript php html angularjs angular

我正在尝试对登录的用户进行身份验证,但我的数据没有进入服务器,所以即使输入正确的电子邮件和密码我也会失败

这是我的组件,

import { Component } from '@angular/core';
import { Router, ROUTER_DIRECTIVES } from '@angular/router';
import { CORE_DIRECTIVES, FORM_DIRECTIVES } from '@angular/common';
import { Http, Headers } from '@angular/http';
import { contentHeaders } from '../headers/headers';
  @Component({

directives: [ ROUTER_DIRECTIVES, CORE_DIRECTIVES, FORM_DIRECTIVES ],
 templateUrl : "./components/login/login.html",

   })
  export class Login {
    constructor(public router: Router, public http: Http) {
  }

  login(event, email, phone) {
   event.preventDefault();
   let body = JSON.stringify({ email, phone });
   var headers = new Headers();


   this.http.post('http://localhost/angular/index.php/profile/logIn',   body, {headers:headers})
  .subscribe(
    response => {
        if(response.json().error_code ==0){
             localStorage.setItem('social_id', response.json().id);
      this.router.navigate(['/demo/profile']);
     // console.log(body);
       alert('hi');
        }
        else{
     alert('fail');
        }
    },
    error => {
      alert(error.text());
      console.log(error.text());
      }
    );
   }
 }

我的模板,

 <div class="login jumbotron center-block">
  <h1>Login</h1>
  <form role="form" (submit)="login($event, email.value, phone.value)">
  <div class="form-group">
   <label for="username">Username</label>
    <input type="text" #email class="form-control" id="emailh" placeholder="Username">
  </div>
 <div class="form-group">
   <label for="password">Password</label>
    <input type="password" #phone class="form-control" id="phoneh" placeholder="Password">
 </div>
 <button type="submit" class="btn btn-default">Submit</button>
   <a [routerLink]="['/signup']">Click here to Signup</a>

    

我认为我在标题上出错了,我不知道有人可以指出错误在哪里......

我的服务器代码(codeigniter)

<?php
header("Access-Control-Allow-Origin: *");

defined('BASEPATH') OR exit('No direct script access allowed');

class Profile extends CI_Controller
{
    public function index()
    {
        $this->load->view('welcome_message');
    }

    public function logIn()
    {

        $email = $this->input->post('email');
        $phone = $this->input->post('phone');
        $this->load->model("Profile_Model");
        $res = $this->Profile_Model->logIn($email, $phone);
        if ($res) {
            $message               = array();
            $message['error_code'] = 0;
            $message['message']    = 'success';
            $message['data']       = $res;
            echo json_encode($message);
            exit;

        } else {
            $message               = array();
            $message['error_code'] = 1;
            $message['message']    = 'fail';
            $message['data']       = new stdClass;
            echo json_encode($message);
            exit;
        }

    }
}

1 个答案:

答案 0 :(得分:1)

我认为你错过了在代码中设置Content-Type标题:

let body = JSON.stringify({ email, phone });
var headers = new Headers();
headers.append('Content-Type', 'application/json');

this.http.post('http://localhost/angular/index.php/profile/logIn',  
    { email, phone }, { headers })
    (...)

您可以注意到,从RC2,可以根据提供的输入隐式设置标头。例如,对于JSON对象,application/json,如下所述:

this.http.post('http://localhost/angular/index.php/profile/logIn',  
    { email, phone })
    (...)