从Angular的Angular SPA到Laravel后端解析更新的数据时出现问题

问题描述 投票:0回答:1

我正在使用在前端使用Angular 8并在后端使用Laravel构建的单页应用程序。 在执行一些CRUD操作时,在编辑部分,我有一个表单,其值是后端用户的值。 当用户更新值时,正在捕获其数据并通过JWT发送到后端,该方法工作正常。

问题是当我使用put方法 (在后端路由上从前端传递更新的数据)时遇到错误。 当我使用post方法时 ,数据会正确通过。

从逻辑上讲,这是错误的,因为更新数据时将put方法与特定资源的ID一起使用。 我需要协助从Angular前端传递带有URL的id,以便可以使用以下路由更新Laravel后端上的数据: Route :: put('editedData / {id}','SubmitFormController @ updateData');

请协助?

Edit.component.html包含具有要更新的值的表单

<form #editForm="ngForm" (ngSubmit)="onSubmit(editForm)">
    <!--Errors-->
    <div class="alert alert-danger" [hidden]="!error">
        {{ error }}
    </div>
<!--Children details-->
<div class="card-header childheader">Update Details</div>
    <div class="card-body">
         <div class="form-group row">
         <div class="col-sm-12">
            <label for="childFirstName">Child Name</label>
            <input 
                type="text" 
                name="childName" 
                class="form-control" 
                [ngModel]="singleUser?.name"
                id="childName" 
                placeholder="Child FirstName">
        </div>
    </div>
    <div class="form-group row">
         <div class="col-sm-6">
             <label for="childAge">Child Age</label>
            <select 
                class="form-control" 
                id="chAge" 
                name="childAge"
                [ngModel]="singleUser?.age" 
                required>
                <option value="" selected disabled> Child's Age</option>
                <option value="1"> 1 </option>
                <option value="2"> 2 </option>
                <option value="3"> 3 </option>
                <option value="4"> 4 </option>
            </select>
        </div>
        <div class="col-sm-6">
            <label for="childGender">Child Gender</label>
            <select 
                class="form-control" 
                id="childGender" 
                name="childGender" 
                [ngModel]="singleUser?.gender" 
                required>
            <option value="" style="display:none"> Child's Gender</option>
                <option value="Male"> Male</option>
                <option value="Female"> Female </option>
            </select>
        </div>
    </div>
       <!--END children-->
    <div class="form-group row">
        <div class="col-sm-12">
            <button 
                type="submit" 
                class="btn btn-lg btn-success btn-block" 
                [disabled]="!editForm.valid">Update Details </button>
        </div>
    </div>
    </div>
</form>

edit.component.ts以捕获表单数据

import { Component, OnInit } from '@angular/core';
import { SharedService } from 'src/app/Services/shared.service';
import { AuthService } from 'src/app/Services/auth.service';

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

  public singleUser : any[];
  public error = null;

  constructor(
    private Shared : SharedService,
    private Auth:AuthService,
  ) { }

  onSubmit(formValue) {
    const capture = {
      UserName: formValue.value.childName,
      Password: formValue.value.childAge,
      Gender: formValue.value.childGender,
    };

    return this.Auth
      .editedData(capture)
      .subscribe(
        data => console.log(data),
        error => console.log(error),
      );
  }

  ngOnInit() {
     this.Shared.edit$.subscribe(message => this.singleUser = message);
  }

}

验证服务以将更新的数据提交到后端

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class AuthService {

  private baseUrl = 'http://localhost/Laravel-anngular-spa/backend/public/api';

  constructor(private http:HttpClient) {}

  editedData(data:any){
    return this.http.post(`${this.baseUrl}/editedData` , data);
  }
}

我想使用的Laravel后端路线

Route::put('editedData/{id}', 'SubmitFormController@updateData');

Laravel后端控制器

 public function updateData(Request $request){
        dd($request->all());
    }
php angular laravel crud edit
1个回答
1
投票

由于您是从Angular前端发出发布请求,而Laravel的路由仅对PUT方法起作用,因此您需要在Angular前端中添加一个_method字段,并将其值用作PUT ,以使Laravel可以将其理解为PUT请求。 请参阅form-method-spoofing

因此,在Angular前端中添加以下内容:

 const capture = {
      UserName: formValue.value.childName,
      Password: formValue.value.childAge,
      Gender: formValue.value.childGender,
      _method : 'PUT'
 };

话虽如此,您也可以尝试使用Angular HttpClient提供的PUT方法。 您很可能会避免上述设置,只需执行此操作this.http.put(//your code).

© www.soinside.com 2019 - 2024. All rights reserved.