简体   繁体   English

Angular 8 HTTP 发布请求失败 404 错误

[英]Angular 8 HTTP Post request failing 404 error

I am trying to build a basic angular phone book application with ability to view, create contacts.我正在尝试构建一个能够查看、创建联系人的基本 angular 电话簿应用程序。 I have used the HTTPClient Post request to create contacts similar to the way demonstrated in Angular heroes tutorial but the post request is failing with following Error "HttpErrorResponse {headers: HttpHeaders, status: 404, statusText: "Not Found", url: " http://localhost:4200/assets/contacts.json ", ok: false, …}" the following is my service file I have used the HTTPClient Post request to create contacts similar to the way demonstrated in Angular heroes tutorial but the post request is failing with following Error "HttpErrorResponse {headers: HttpHeaders, status: 404, statusText: "Not Found", url: " http ://localhost:4200/assets/contacts.json ", ok: false, ...}" 以下是我的服务文件

    import { HttpClient } from '@angular/common/http';
    import { Contact } from './contact';
    import { Observable } from 'rxjs';

    @Injectable({
      providedIn: 'root'
    })
    export class ContactInfoService {

      constructor(private httpClient: HttpClient) { }

      contactUrl = 'assets/contacts.json';

      getContacts(): Observable<Contact []> {
        return this.httpClient.get<Contact []>(this.contactUrl);
      }

      postContact(contact: Contact): Observable<Contact> {
        return this.httpClient.post<Contact>(this.contactUrl,contact);
      } 

    }

this one is component.ts这个是component.ts

import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, FormControl} from '@angular/forms';
import { Contact } from '../contact';
import { ContactInfoService } from '../contact-info.service';


@Component({
  selector: 'app-contact-model',
  templateUrl: './contact-model.component.html',
  styleUrls: ['./contact-model.component.css']
})
export class ContactModelComponent {
   contactForm: FormGroup;
  contacts: Contact[];

  constructor(
    private formBuilder: FormBuilder,
    private contactInfoService: ContactInfoService
  ) {
      this.contactForm = new FormGroup({
        firstName: new FormControl(''),
        lastName: new FormControl(''),
        eMail: new FormControl(''),
        phoneNumber: new FormControl(''),
        status: new FormControl('')
      });
   }



  postContact(): void {

    var newContact: Contact;
    newContact = {}; 
    newContact.firstName = this.contactForm.get('firstName').value;
    newContact.lastName = this.contactForm.get('lastName').value;
    newContact.email = this.contactForm.get('eMail').value;
    newContact.phoneNumber = this.contactForm.get('phoneNumber').value;
    newContact.status = this.contactForm.get('status').value;
    console.log(newContact);

    this.contactInfoService.postContact(newContact).subscribe(contact => this.contacts.push(contact));
  }

  getContacts(): void {
    this.contactInfoService.getContacts().subscribe(contacts => (this.contacts = contacts));
  }

}

this one is html file这个是 html 文件

<div class="jumbotron">
<h3>New Contact</h3>

<form [formGroup]= "contactForm">
    <div>
        <label for="firstName">
            First Name
        </label><br>
        <input id="firstName" type="text" formControlName="firstName">
    </div>

    <div>
        <label for="lastName">
            Last Name
        </label><br>
        <input id="lastName" type="text" formControlName="lastName">
    </div>

    <div>
        <label for="eMail">
            Email
        </label><br>
        <input id="eMail" type="email" formControlName="eMail">
    </div>

    <div>
        <label for="phoneNumber">
            Phone Number
        </label><br>
        <input id="phoneNumber" type="tel" formControlName="phoneNumber" pattern="[0-9]{10}">
    </div>

    <div>
        <label>
            Status
        </label><br>
        <input id="Active" type="radio" value="Active" formControlName="status">
        <label for="Active">Active</label><br>
        <input id="Inactive" type="radio" value="Inactive" formControlName="status">
        <label for="Inactive">Inactive</label><br>
    </div>


    <button type="submit" (click)="postContact(); getContacts()">Add</button>
</form>
</div>

this is json file这是 json 文件

[
    { 
     "id": "1",
     "firstName":"abc",
     "lastName":"cba",
     "email":"abc_cba@abc.com",
     "phoneNumber":"1234567890",
     "status":"active"
     },
     { 
        "id": "2",
        "firstName":"def",
        "lastName":"fed",
        "email":"def_fed@def.com",
        "phoneNumber":"0987654321",
        "status":"inactive"
    }
 ]

When I click the Add button, in networks tab I see the post request with status 404. I have found several questions similar to this but none have worked for me.当我单击“添加”按钮时,在“网络”选项卡中,我看到状态为 404 的发布请求。我发现了几个与此类似的问题,但没有一个对我有用。 If you have any ideas how to fix this issue or experienced the similar problem - any help or comment will be highly appreciated.如果您有任何解决此问题的想法或遇到类似问题 - 任何帮助或评论将不胜感激。

Thank you谢谢

You are getting a 404 error which means a page not found error.您收到 404 错误,这意味着找不到页面错误。 Therefore there is a good chance that the endpoint you are referring from the angular app does not exist.因此,您从 angular 应用程序引用的端点很可能不存在。 Therefore please check the contactUrl again or just check the endpoint with postman and paste the URL and see if it works.因此,请再次检查contactUrl,或者只检查端点postman并粘贴URL,看看它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM