【问题标题】:How to use put method properly ANGULAR如何正确使用 put 方法 ANGULAR
【发布时间】:2021-12-22 12:26:36
【问题描述】:

我有以下代码:

HTML:

<input 
 #title
(keyup.enter)="createPost(title)"
type="text" 
class="form-control">
<ul class="list-group">
    <li *ngFor="let post of posts" class="list-group-item">
        {{ post.title }}
        <button class="btn btn-default btn-sm" (click)="updatePost(post)">Update</button>
    </li>
</ul>

角度:

export class PostsComponent implements OnInit {
  posts: any;
  url = 'https://jsonplaceholder.typicode.com/posts'

  constructor(private http: HttpClient) {
    http.get(this.url)
    .subscribe(response => {
      this.posts = response;
    })
  }

----

updatePost(post: any) {
    this.http.put(this.url + '/' + post.id, JSON.stringify({ title: 'Example' }))
    .subscribe(response => {
        
    })
  }

如何使用标题“示例”更新给定 ID 的帖子值? 如果我 console.log(response) 它只返回一个 ID,而不是一个正确的值。 请帮忙:)

【问题讨论】:

    标签: javascript html angular frontend


    【解决方案1】:

    他们似乎在请求标头中期待内容类型的 application/json。您可以使用 content-type 并查看结果https://reqbin.com/o0tzomnc 但即使您指定内容类型,它们也只会返回更新的字段。在您的情况下 - 只有标题字段

    【讨论】:

      猜你喜欢
      • 2015-09-16
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 2020-09-24
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多