HTTP PUT方法返回未找到的错误

时间:2017-11-06 04:34:03

标签: javascript json angular html-table put

尝试执行PUT方法时出错。错误是这样的: PUT http://localhost:3000/Student/ 404 (Not Found)

以下是stock.service.ts文件的片段

updStudent(id: string, newName: string, newYear: string, newSemester: string, newScore: string): Observable<any>
    {
        console.log(JSON.stringify({
            name: newName, 
            year: newYear, 
            semester: newSemester,
            score: newScore
        }));
        return this.http.put("http://localhost:3000/Student/", +id,
        JSON.stringify({
                name: newName, 
                year : newYear, 
                semester : newSemester,
                score: newScore
            }));
     }

这是app.component.ts文件的片段

selectedStudent: any;

updStudent(Id: string, newName: string, newYear: string, newSemester: string, newScore: string)
  {
    this.stockService.updStudent(this.selectedStudent.Id, newName, newYear, newSemester, newScore).subscribe(

      data =>
      {
        this.getAllStocks();
      }

    );
    console.log('here', Id , newName, newYear, newSemester, newScore);
  }

任何人都可以帮我解决这个错误吗?如果需要更多代码段,请告诉我。谢谢。

Link full ts

1 个答案:

答案 0 :(得分:1)

被称为的Api不支持Put()调用。您应该使用Post Method调用

updStudent(id: string, newName: string, newYear: string, newSemester: string, newScore: string): Observable<any>
    {
        console.log(JSON.stringify({
            name: newName, 
            year: newYear, 
            semester: newSemester,
            score: newScore
        }));
        return this.http.post("http;//localhost:3000/Student/", +id,
        JSON.stringify({
                name: newName, 
                year : newYear, 
                semester : newSemester,
                score: newScore
            }));
     }