在angular2中验证用户失败


Getting fail to authenicate the user in angular2

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

这是我的组件

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;
        }
    }
}

我认为您错过了在代码中设置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 })
    (...)