Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用指定的内容类型在Angular中执行post请求_Angular_Http - Fatal编程技术网

如何使用指定的内容类型在Angular中执行post请求

如何使用指定的内容类型在Angular中执行post请求,angular,http,Angular,Http,我想在angular 4中执行一个http post请求,它在Postman中运行良好,示例如下: 标题: 正文: 代码如下: resetPasswordCall(输入,jwt){ 返回此.httpClient.post(AppSettings.API_CACHE+'scoraChangePassword/'+jwt,输入{ headers:new-HttpHeaders().set('Content-Type','application/x-www-form-urlencoded'),

我想在angular 4中执行一个http post请求,它在Postman中运行良好,示例如下:

标题:

正文:

代码如下:

resetPasswordCall(输入,jwt){
返回此.httpClient.post(AppSettings.API_CACHE+'scoraChangePassword/'+jwt,输入{
headers:new-HttpHeaders().set('Content-Type','application/x-www-form-urlencoded'),
}).map(res=>res);

}
在app.service.ts文件中

import { Injectable } from '@angular/core';
import { Http,Response,Headers } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class UserActionsService {
  user_action_url: string;
  data : any={'success':'',error:''};
  constructor(private _http: Http) { }

  post_method() {
    var  headers = new Headers;
    headers.append('Content-Type','application/json; charset=utf-8');
      return this._http.post(this.user_action_url,this.data,{headers:headers}).map(res=>res);
  }
}
在login.component.ts中

import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  User : Logindetails;
  user_data: any;
  token: string;
  constructor( private useraction_service : AppService,) {
     }

  ngOnInit() {
    this.User = {
      username : "",
      email : "",
      password : ""
    }
  }
  loginform(User) {
    this.useraction_service.user_action_url = 'http://localhost:3000/login/';
    this.useraction_service.data = this.User;
    this.useraction_service.post_method().subscribe(res => {
      this.user_data = res.json();
      if(this.user_data.status == 2 ) {
        console.log('wrong credentials');
      } else {
          console.log('logged in successfully');
          }
      }
    },(error: any) => {
        console.log(error);
    }
  );
  }
}
interface Logindetails {
  email : String;
  password : String;
}
app.component.html

<form class="example-form" name="loginform" #login="ngForm" (ngSubmit)="loginform(User)" novalidate>
    <mat-form-field>
      <input matInput name="emailid" [(ngModel)]="User.email" [(ngModel)]="User.username"  #email="ngModel" placeholder="Enter your username or Email ID" required>
    </mat-form-field>
    <mat-form-field>
      <input matInput name="password" [(ngModel)]="User.password" #email="ngModel" placeholder="Enter your Password" required>
    </mat-form-field>
    <div class="register_btn">
      <button type="submit" mat-raised-button color="accent" [disabled]="!login.form.valid">Login</button>
    </div>  
  </form>

登录

您需要订阅observable以实际执行http请求


希望这对您有所帮助

谢谢您的回复

我找到了正确的答案

resetPasswordCall(输入,jwt){
//像这样增加身体
var body='newPassword='+input.newPassword+'&confirmPassword='+input.confirmPassword;
返回此.httpClient.post(AppSettings.API_CACHE+'scoraChangePassword/'+jwt,body{
//设置标题
headers:new-HttpHeaders().set('content-type','application/x-www-form-urlencoded'),
}).map(res=>res);

}
您使用的确切版本是什么?”@angular/core:“^4.4.4”,看起来您的请求是正确的,我认为前端出现问题,无法正确处理github问题中的res数据,您尝试过这个吗?:@MohammadRaheem,是的,尝试过,但不起作用。