如何将参数作为查询字符串传递给Angular 5中的httpClient.post?

时间:2018-05-10 07:37:11

标签: angular httpclient

我使用Header将参数作为查询字符串传递给HttpClient.post  '内容类型':' application / x-www-form-urlencoded'

我检查了之前在Stack溢出中提出的许多问题,但没有取得成功..

在service.ts

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

searchPerson(contactNo) {
  let header = new HttpHeaders(); 
    header = header.append('Content-Type','application/x-www-form-urlencoded');

  let params = new HttpParams();
   params =params.set ('grant_type','password');
   params =params.set ('username',contactNo);

   return this.http.post('Some Url',params,{headers:header });
 }

在login.component.ts

 import { Component, OnInit } from '@angular/core';
 //import {Observable} from 'rxjs/Rx';
import {FactoryService} from '../../services/factory.service';

  constructor(private factory: FactoryService) { }

logMeIn(contactNo:string){
 this.loading = true;
   this.factory.searchPerson(this.contactNo).subscribe(
    data=> { console.log(data.token);},
    err=> {    
              this.loading = false;
              console.log(err.message); 
          },
    ()=>{
          this.loading = false;
          console.log('Login process done');
          }
  );
}

抛出异常:URL的Http失败响应:400 Bad Request

0 个答案:

没有答案