【发布时间】:2017-11-11 15:18:36
【问题描述】:
我正在尝试使用 http.put 更新我的 API 中的一些数据,但我总是得到“404 not found”。这是我的代码:
TS 文件:
activate(customer) {
if(this.mycode==this.cuscode) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify(customer);
this.http.put('http://myApi.com/customer/' + this.id, body, options )
.map(res => res.json())
.subscribe(data => {
console.log("Successfully Activated your Account.");
}, (err) => {
console.log("ERROR: " +err);
});
}else {
this.wrongcode = true;
console.log("NOT ACTIVATED! Incorrect code mate.");
}
}
HTML 文件:
<ion-item [(ngModel)]="mycode">
<ion-input type="text" placeholder="Activation Code" required></ion-input>
</ion-item>
<button ion-button (click)="activate(customer)"><ion-icon name="check"></ion-icon>Activate Now</button>
我得到这个错误:
PUT http://myApi/customer/72 404 (Not Found)
我的 API 工作正常,但我不知道为什么找不到它?希望你们能帮助我。提前谢谢你。
【问题讨论】:
-
你能从服务器检查你的api方法是否有“put”而不是“get”。也可以尝试从高级休息客户端或任何其他休息客户端点击这个。
-
感谢您的回复,但是从服务器检查是什么意思?
-
尝试在你的开发工具中使用网络标签,通过console.log检查你的this.mycode和this.cucode
-
@DmitryGrinko,if 条件有效,this.mycode 和 cuscode 是输入的代码和验证码。感谢您的回复。
-
我曾经遇到过同样的问题,API 被暴露为“get”,所以当我尝试从浏览器中点击它时,它曾经向我发送结果,但“put”方法没有暴露,因为一个例子,如果这是 nodejs api 代码应该像这样 expressRoute.put('/your/put/route', function(res,req){...}),在我的例子中它被暴露为 expressRoute.get( '/your/put/route',...),所以只需从后端团队确认,看看它是否以正确的方式暴露。