angularメモ帳

angular始めました。
動かすために調べたことをメモしています。

702 views

HTTPClientでPOST

送信時のデータ形式はjson形式じゃなくて、urlencoded形式の場合。

import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-blas-auth',
  templateUrl: './blas-auth.component.html',
  styleUrls: ['./blas-auth.component.css']
})


export class BlasAuthComponent implements OnInit {

  message:string="";

  loginForm: FormGroup = new FormGroup({
    userName: new FormControl(''),
    password: new FormControl('')});


  constructor(private client: HttpClient) { }

  ngOnInit(): void {

  }

  onSubmit() {
    let result = this.loginForm?.value;
    console.log(result.userName);
    console.log(result.password);

    let body = new HttpParams()
    .set("name", result.userName)
    .set("password", result.password);
    //let body = JSON.stringify(result);
    //console.log(this.message);

    let httpOptions = {
      headers: new HttpHeaders({
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/x-www-form-urlencoded',
      })
    }

    this.client.post("http://localhost/api/v1/auth/login/", body)
    .toPromise()
    .then((res)=>{
      console.log(res);
    })
    .catch((error)=>{

    });
  }

}

Page 22 of 38.

前のページ 次のページ



[添付ファイル]

1.angular flex layoutについて.docx  


お問い合わせ

プロフィール

すぺぺぺ

自己紹介

本サイトの作成者。
プログラムは趣味と勉強を兼ねて、のんびり本サイトを作っています。
フレームワークはdjango。
ChatGPTで自動プログラム作成に取り組み中。

サイト/ブログ

https://www.osumoi-stdio.com/novel/

ツイッター

@darkimpact0626