【问题标题】:Requisition method PUT angular申请方法 PUT angular
【发布时间】:2021-06-02 17:25:44
【问题描述】:

enter image description here我有以下问题,当我尝试放入 api 时,服务器返回错误 405。 你能帮助我吗? 下面是代码,因此您可以检查是否在我的代码中看到任何可能导致此错误的错误... 我相信组件的打字稿可能有问题。 我认为组件的 Typescript 可能有问题

Service Typescript


  getUser(id: any): Observable<ResponseUse> {
    const _url = `${this.apiClient}/${id}`
    return this.http.get<ResponseUse>(_url);
  }

  updateUser(id: any, request: ResponseUpdate): Observable<ResponseUpdate> {
    const _url = `${this.apiClient}/${id}`
    return this.http.put<ResponseUpdate>(_url, request);
  }
COMPONENT UPDATE 

<div class="backgroud">
  <h1>Update user</h1>
    <div class="label" *ngIf="request">
      <label>
        ID:
        <input placeholder="ID" [(ngModel)]="request.id" />
      </label>
      <label>
        Name:
        <input placeholder="Name" [(ngModel)]="request.name" />
      </label>
      <label>
        Email:
        <input placeholder="Email" [(ngModel)]="request.email" />
      </label>
      <label>
        Telephone:
        <input placeholder="Telephone" [(ngModel)]="request.telephone" />
      </label>
    </div>
  
    <div class="btn">
      <button mat-raised-button color="primary"  (click)="update()">
      Update
      </button>
      <button mat-stroked-button routerLink="/">Cancel</button>
  
    </div>
  </div>
  
  
  
  TYPESCRIPT COMPONENT 
  
  import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { RequestUpdate } from 'src/app/model/live.model';
import { apiService } from 'src/app/services/api.service';

@Component({
  selector: 'app-client-update',
  templateUrl: './client-update.component.html',
  styleUrls: ['./client-update.component.css']
})
export class ClientUpdateComponent implements OnInit {

  id: any;
  request!: RequestUpdate;
  
  constructor(
    private clientService: apiService,
    private route: ActivatedRoute) { }

  ngOnInit() {
    this.id = this.route.snapshot.paramMap.get('id');
    this.clientService.getUser(this.id).subscribe(res =>{
      this.request = {
        id: res.id,
        name: res.name,
        email: res.email,
        telephone: res.telephone
      }
    })
  }

  update(){
    this.clientService.updateUser(this.id, this.request).subscribe(res => {
      console.log(`${this.request}`)
    })
  }
}

  

【问题讨论】:

  • 检查您的服务器端代码是否允许在资源上放置 PUT?
  • 是的,PUT方法是允许的,下面我附上google的错误截图
  • 我在正文开头加了一张照片
  • 您是否尝试使用邮递员访问此网址?

标签: angular typescript put


【解决方案1】:

响应代码 405 表示您请求的端点不允许使用该方法 (PUT)。

仔细检查 PUT 方法是否被接受,以及您发送请求的网址是否准确(没有拼写错误等)。还值得检查浏览器上的开发者控制台,以确保正确编译完整的端点

[编辑] 405 状态码 MDN:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

http 状态码:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

【讨论】:

  • 但是我相信我犯了这个错误,当使用站点resttesttest.com 在 PUT 方法中执行测试时,我收到错误 405 和消息 content-length:0
  • 我在正文开头加了一张照片
猜你喜欢
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 2017-04-29
  • 1970-01-01
相关资源
最近更新 更多