angularjs Angular PUT请求问题-“Required request body is missing”

zkure5ic  于 2024-01-05  发布在  Angular
关注(0)|答案(1)|浏览(204)

我遇到了一个Angular应用程序的问题,我试图向我的服务器发送一个PUT请求,但我得到了错误消息“Required request body is missing”。
下面是相关的代码片段:
子组件(ChambreFormComponent):

  1. <div class="h-screen md:flex">
  2. <div
  3. class="relative overflow-hidden md:flex w-1/2 bg-gradient-to-tr from-blue-800 to-purple-700 i justify-around items-center hidden"
  4. >
  5. >
  6. <img class="h-40 w-96 mr-40" src="{{ image }}" />
  7. <div
  8. class="absolute -bottom-32 -left-40 w-80 h-80 border-4 rounded-full border-opacity-30 border-t-8"
  9. ></div>
  10. <div
  11. class="absolute -bottom-40 -left-20 w-80 h-80 border-4 rounded-full border-opacity-30 border-t-8"
  12. ></div>
  13. <div
  14. class="absolute -top-40 -right-0 w-80 h-80 border-4 rounded-full border-opacity-30 border-t-8"
  15. ></div>
  16. <div
  17. class="absolute -top-20 -right-20 w-80 h-80 border-4 rounded-full border-opacity-30 border-t-8"
  18. ></div>
  19. </div>
  20. <div class="flex md:w-1/2 justify-center py-10 items-center bg-white">
  21. <form class="bg-white" (ngSubmit)="onSubmit()">
  22. <h1 class="text-black font-bold text-2xl mb-1">{{ titre }}</h1>
  23. <!-- <p class="text-sm font-normal text-gray-600 mb-7">Welcome Back</p> -->
  24. <div class="flex items-center border-2 py-2 px-3 rounded-2xl mb-4">
  25. <svg
  26. xmlns="http://www.w3.org/2000/svg"
  27. class="h-5 w-5 text-gray-400"
  28. viewBox="0 0 20 20"
  29. fill="currentColor"
  30. >
  31. <path
  32. fill-rule="evenodd"
  33. d="M10 9a3 3 0 100-6 3 3 0 000 6zm-7 9a7 7 0 1114 0H3z"
  34. clip-rule="evenodd"
  35. />
  36. </svg>
  37. <input
  38. class="pl-2 outline-none border-none"
  39. type="text"
  40. name=""
  41. id=""
  42. [(ngModel)]="formData.numeroChambre"
  43. placeholder="{{ roomnumber }}"
  44. [ngModelOptions]="{ standalone: true }"
  45. />
  46. </div>
  47. <div class="flex items-center border-2 py-2 px-3 rounded-2xl mb-4">
  48. <svg
  49. xmlns="http://www.w3.org/2000/svg"
  50. class="h-5 w-5 text-gray-400"
  51. fill="none"
  52. viewBox="0 0 24 24"
  53. stroke="currentColor"
  54. >
  55. <path
  56. stroke-linecap="round"
  57. stroke-linejoin="round"
  58. stroke-width="2"
  59. d="M12 11c0 3.517-1.009 6.799-2.753 9.571m-3.44-2.04l.054-.09A13.916 13.916 0 008 11a4 4 0 118 0c0 1.017-.07 2.019-.203 3m-2.118 6.844A21.88 21.88 0 0015.171 17m3.839 1.132c.645-2.266.99-4.659.99-7.132A8 8 0 008 4.07M3 15.364c.64-1.319 1-2.8 1-4.364 0-1.457.39-2.823 1.07-4"
  60. />
  61. </svg>
  62. <input
  63. class="pl-2 outline-none border-none"
  64. type="text"
  65. name=""
  66. id=""
  67. [(ngModel)]="formData.typeC"
  68. placeholder="{{ typeRoom }}"
  69. [ngModelOptions]="{ standalone: true }"
  70. />
  71. </div>
  72. <button
  73. type="submit"
  74. class="block w-full bg-indigo-600 mt-4 py-2 rounded-2xl text-white font-semibold mb-2"
  75. >
  76. {{buttonText}}
  77. </button>
  78. </form>
  79. </div>
  80. </div>

字符串
.ts

  1. import {
  2. Component,
  3. EventEmitter,
  4. Input,
  5. Output,
  6. ViewChild,
  7. } from '@angular/core';
  8. import { ChambreFormUpdateComponent } from '../chambre-form-update/chambre-form-update.component';
  9. @Component({
  10. selector: 'app-chambre-form',
  11. templateUrl: './chambre-form.component.html',
  12. styleUrls: ['./chambre-form.component.css'],
  13. })
  14. export class ChambreFormComponent {
  15. @Input() image = '';
  16. @Input() buttonText = '';
  17. @Input() titre = '';
  18. @Input() roomnumber = 0;
  19. @Input() typeRoom = '';
  20. @Input() formData: any;
  21. @Output() submitForm = new EventEmitter<any>();
  22. constructor() {
  23. console.log('Chambre Form Update titre' + this.titre);
  24. }
  25. onSubmit() {
  26. // Emit the form data when the form is submitted
  27. this.submitForm.emit();
  28. }
  29. }


父组件(ChambreFormUpdateComponent):

  1. <app-chambre-form
  2. [image]="image"
  3. [titre]="title"
  4. [buttonText]="buttonText"
  5. [typeRoom]="type"
  6. [roomnumber]="roomNum"
  7. [formData]="updateForm.value"
  8. (submitForm)="updateRoom(updateForm.value.idChambre, $event)"
  9. ></app-chambre-form>`


.ts:

  1. import { Component, ViewChild } from '@angular/core';
  2. import { ChambreFormComponent } from '../chambre-form/chambre-form.component';
  3. import { environment } from 'src/environments/environment.development';
  4. import { HttpClient } from '@angular/common/http';
  5. import { FormBuilder } from '@angular/forms';
  6. import { Router } from '@angular/router';
  7. @Component({
  8. selector: 'app-chambre-form-update',
  9. templateUrl: './chambre-form-update.component.html',
  10. styleUrls: ['./chambre-form-update.component.css'],
  11. })
  12. export class ChambreFormUpdateComponent {
  13. rooms: any[] = [];
  14. image = './assets/chambres/updateRoom.png';
  15. title = 'Update Room';
  16. buttonText = 'Update Room';
  17. constructor(
  18. private http: HttpClient,
  19. private fb: FormBuilder,
  20. private router: Router
  21. ) {
  22. console.log('valueeeee' + this.updateForm.value);
  23. }
  24. updateForm: any = {
  25. value: {
  26. numeroChambre: 0,
  27. typeC: '',
  28. },
  29. };
  30. type = this.updateForm.value.type;
  31. roomNum = this.updateForm.value.numero;
  32. updateRoom(roomId: number, updatedRoom: any) {
  33. const apiUrl = environment.BaseUrl + 'chambre/modifierchambre';
  34. this.http.put<any>(apiUrl, updatedRoom).subscribe({
  35. next: (data) => {
  36. console.log('Room updated successfully:', data);
  37. const index = this.rooms.findIndex((room) => room.idChambre === roomId);
  38. if (index !== -1) {
  39. this.rooms[index] = data;
  40. }
  41. },
  42. });
  43. }
  44. openUpdateForm(room: any) {
  45. room.showUpdateForm = true;
  46. this.updateForm.value = { ...room };
  47. }
  48. }


当发出PUT请求时,我收到一个“Required request body is missing”错误。我怀疑这个问题可能与我如何处理表单数据或构造请求主体有关。
有人能帮我找出可能导致这个问题的原因吗?我应该如何在我的Angular应用程序中正确构造和发送请求正文?

yk9xbfzb

yk9xbfzb1#

你能不能把updateRoom方法改成下一个,然后检查结果。我猜问题出在Content-Type上,因为通常它需要json,但你发送了string

  1. updateRoom(roomId: number, updatedRoom: any) {
  2. const apiUrl = environment.BaseUrl + 'chambre/modifierchambre';
  3. this.http.put<any>(apiUrl, updatedRoo(
  4. {
  5. headers : new HttpHeaders({'Content-Type': 'application/json'})
  6. }).subscribe({
  7. next: (data) => {
  8. console.log('Room updated successfully:', data);
  9. const index = this.rooms.findIndex((room) => room.idChambre === roomId);
  10. if (index !== -1) {
  11. this.rooms[index] = data;
  12. }
  13. },
  14. }));
  15. }

字符串

展开查看全部

相关问题