Angular 8:POST http:// localhost:4200 / assets / data / students.json 404(未找到)

LJ27

我正在做一个项目,该项目可以使用HTTP来获取数组数据,并且应该也可以使用HTTP添加新数组,但是每次尝试发布新数组时,我都会得到:

POST http:// localhost:4200 / assets / data / students.json 404(未找到)

这很奇怪,因为我能够从url中获取数据而不会出错,但是无法在其中发布新数组。

这是我的项目的结构:

在此处输入图片说明

student.service.ts:

import { Injectable } from '@angular/core';
import {HttpClient,HttpErrorResponse,HttpHeaders} from '@angular/common/http';
import { IStudent} from './student'
import { Observable, of } from 'rxjs'
 import { catchError,map, tap } from 'rxjs/operators'
@Injectable({
  providedIn: 'root'
})
export class StudentService {

private  _url:string = "/assets/data/students.json";

constructor(private _http : HttpClient) { }

   delete(id: number) {
     return this._http.delete(`/student/` + id);
 }

 getStudents():Observable<IStudent[]>{
   return this._http.get<IStudent[]>(this._url);
 }


  addStudent(student : IStudent ):Observable<IStudent>{
    return this._http.post<IStudent>(this._url,student);
  }




}

学生

export class IStudent{
  id:number;
  name:string;
  age:number
}

/assets/data/students.json

[
  {"id":1,"name":"John","age":22},
  {"id":2,"name":"Austine","age":26},
  {"id":3,"name":"Samantha","age":24},
  {"id":4,"name":"Lily","age":25}
]

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule,routingComponents } from './app-routing.module';
import { AppComponent } from './app.component';
import { StudentListComponent } from './student-list/student-list.component';
import { StudentDetailComponent } from './student-detail/student-detail.component';
import { FormsModule } from '@angular/forms'
import {StudentService} from './student.service';
import {HttpClientModule} from '@angular/common/http';

@NgModule({
  declarations: [
    AppComponent,
    StudentListComponent,
    StudentDetailComponent,
    routingComponents
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    HttpClientModule,
  ],
  providers: [StudentService],
  bootstrap: [AppComponent]
})
export class AppModule { }

学生详细信息

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute,Router,ParamMap } from '@angular/router';
import {StudentService} from '../student.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import {IStudent} from '../student';
import { HttpClient } from "@angular/common/http";
@Component({
  selector: 'app-student-detail',
  template: `
  <h2>Student Form</h2>
  <ul class="student" >
      <li   *ngFor = "let student of students">
      <span class="badge">{{student.id}} {{student.name}} {{student.age}}</span>
  </li>
</ul>
  <form >
  <div class="form-group">
      <label>ID:</label>
      <input #SID type="number"  class="form-control" id="id" name="id">
</div>

    <div class="form-group">
      <label>Name:</label>
      <input  #SName type="text"  class="form-control" id="name" name="name">
</div>
<div class="form-group">
      <label>Age:</label>
      <input #SAge type="number"  class="form-control" id="name" name="name">
</div>

<button (click)="add(SID.value,SName.value,SAge.value)" type="submit">Add</button>
</form >


 `,



  styleUrls: ['./student-detail.component.css']
})
export class StudentDetailComponent implements OnInit {

 public students = [];
public studentId;
public studentName;
public studentAge;
registerForm : FormGroup;


  constructor(private route : ActivatedRoute,private router: Router,private _studentService: StudentService) { }


  ngOnInit() {
    this._studentService.getStudents()
      .subscribe(data => this.students = data);
      }

        add(id:number,name:string,age:string): void {
         this._studentService.addStudent({ id } as IStudent)
           .subscribe(student => {
             this.students.push(student);

           });
       }
      }
杜莱

您正在尝试写入本地文件。Java脚本不支持在本地文件上写入。

因此,代替JSON在服务中创建变量类型数组,并编写方法以将其推送到Get上。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

POST http:// localhost:4200 / contact / send 404(未找到)

使用angular-cli在angular2中获取http:// localhost:4200 / null 404(未找到)

(404未找到)正在加载http:// localhost:4200 / angular2-datatable / datatable

在 Angular 中获取 http://localhost:4200/[object%20Blob] 404(未找到)

GET http://localhost:4200/src/app/ficheros/nacionalidades.json 404(未找到)

Angular2和节点js中的POST http:// localhost:4200 / addTask 404(未找到),但值已在控制台中打印

POST http:// localhost:3000/404(未找到)

“http://localhost:49540/api/create/ 的 HTTP 失败响应:404 未找到”Angular 11 POST 请求

GET http:// localhost:4200 / resource 404(未找到)

POST http://localhost:55703/ValidStep1 404(未找到)

Angular 2 + angular-in-memory-web-api区域GET http:// localhost:3000 / traceur 404(未找到)

http:// localhost / undefined 404(未找到)

在angular4中获取http:// localhost:3000 / node_modules / rxjs / 404(未找到)

GET http:// localhost:3000/404(未找到),实际上是在拨打POST却获得GET

GET http:// localhost:4200 / node_modules / zone.js / dist / zone.js 404(未找到)

在組件 service.ts url: "http://localhost:4200/data/products.json" not found

GET http://localhost:3000/:/localhost:4000 404(未找到)

GET http://localhost:5000/auth/me 404(未找到)

GET http://localhost:5000/error 404 (未找到)

PUT http://localhost:3000/events/undefined 404(未找到)

GET http://localhost:3000/hello 404(未找到)

未找到 Http 404

angular 应用程序适用于 http://localhost:4200,但我想要 https://localhost:44325

GET http:// localhost:3000 / components / t.mp3 net :: ERR_ABORTED 404(未找到)

<未定义:1 GET http:// localhost:63342 / Twitchtv /未定义404(未找到)>

XHR错误(404未找到)正在加载http:// localhost:3000 / traceur

ionic3 GET http:// localhost:8100 / null 404(未找到)

离子2 GET http:// localhost:8100 / search / aq?query = 404(未找到)

我收到错误 GET http://localhost:3000/api/products 404(未找到)