angular - 为什么 GET 工作,但 DELETE 和 POST 在 Angular 2 中不起作用(错误:404)?
问题描述
实际上我的http工作,get
工作,但delete
没有post
工作。你觉得我会怎么做?
这是我的组件,我不知道为什么它不起作用
组件.ts
import { Component, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import { HttpService } from '../http.service';
@Component({
selector: 'app-http',
templateUrl: './http.component.html',
styleUrls: ['./http.component.css']
})
export class HttpComponent implements OnInit {
followers = [];
constructor(private hService: HttpService,
private http: Http
) { }
private url: string = "./app/http/followers.json";
ngOnInit() {
this.http.get(this.url)
.map((response: Response) => response.json())
.subscribe(user=>this.followers=user)
}
createpost(input: HTMLInputElement) {
let name = { name: input.value };
input.value = '';
this.http.post(this.url, JSON.stringify("name"))
.subscribe(response => {
name = response.json();
this.followers.splice(0, 0, name);
});
}
deletepost() {
this.http.delete(this.url + '/' + "id")
.subscribe(response => {
let index = this.followers.indexOf("id");
this.followers.splice(index, 1);
});
}
}
这是我的 http>follower.json
组件.html
<div>
<h2>Followers list</h2>
<input type="text" #name (keyup.enter)="createpost(name)" class="list-control">
<button class="btn btn-primary" (click)="createpost(name)">Add to fallowers</button>
<ul class="list-group">
<li *ngFor="let follower of followers" class="list-group-item">
<button class="btn btn-danger" (click)="deletepost(name)">UnFollow</button>
{{follower.name}}
</li>
</ul>
</div>
解决方案
推荐阅读
- c - io_setup 中的 nr_events 与 io_submit 和 io_getevents 中的 nr 是什么关系?
- java - Employee 类型的方法 getEmailId() 未定义
- node.js - 如何在 GAE 上部署 angular9
- javascript - 如果值为 null 或不存在,则打印字符串
- angular - Angular SPA 在 Google PageSpeed Insights 上返回 404
- c# - C# - 输入字符串的格式不正确(Set-UserPhoto)
- ios - 不能在 SwiftUI 中使用工作表将数据添加到核心数据
- r - 在R中将日期时间转换为日期
- image-processing - 如何在具有不同地面实况和输出通道的多类分割问题中计算损失?(UNET,骰子损失)
- c# - 如何在视图上使用 asp.net mvc 中的两个模型?