在角度5中,通过http发送数据

时间:2018-06-15 12:22:41

标签: angular5

我发送登录后的数据(手机和密码)。但我没有得到回应。下面我附上了两个文件1)sign-in.component.ts和2)user.service.ts。单击提交按钮而不是调用OnSubmit函数(sign-in.component.ts)

登录-in.component.ts

import { Component, OnInit } from '@angular/core';
import { UserService } from '../../shared/user.service';
import { HttpErrorResponse } from '@angular/common/http';
import { Router } from '@angular/router';
@Component({
  selector: 'app-sign-in',
  templateUrl: './sign-in.component.html',
  styleUrls: ['./sign-in.component.css']
})
export class SignInComponent implements OnInit {
  isLoginError : boolean = false;
  constructor(private userService : UserService,private router : Router) { } 
  ngOnInit() {
  }
  OnSubmit(userName,password){

    console.log(userName + password);
     this.userService.userAuthentication(userName,password)
     .subscribe((data : any)=>{ 
      console.log(data);
      //localStorage.setItem('userToken',data.access_token);
      this.router.navigate(['/home']);
    },
    (err : HttpErrorResponse)=>{

      console.log(HttpErrorResponse);
      this.isLoginError = true;
    });
  }

}

user.service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Response } from "@angular/http";
import { Observable } from 'rxjs';

//import { Http, Response, RequestOptions, Headers } from '@angular/http';
//import 'rxjs/add/operator/map';
import { User } from './user.model';

@Injectable()
export class UserService {
  readonly rootUrl = 'http://192.168.0.112:3001/admin/login';
  constructor(private http: HttpClient) { }

  userAuthentication(userName, password) {
    var data = "mobile=" + userName + "&password=" + password ;
    console.log(data);
    var reqHeader = new HttpHeaders({ 'Content-Type': 'application/json','No-Auth':'True' });

     const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json',
    'Authorization': 'my-auth-token'
  })
};

    return this.http.post('http://192.168.0.112:3001/admin/login',data,httpOptions);
  }



}

0 个答案:

没有答案