创建和更新JSON调用不起作用

时间:2017-01-09 23:40:04

标签: angularjs json json-server

我正在学习Angular 2并且有一个有趣的问题。我正在使用json-server模拟我的服务调用并检索工作正常。但是我在创建和更新方面存在问题。

我正在使用一个名为notification的基本模型,如下所示: -

export class NotificationModel {
constructor(
    public id: number,
    public name: string,
    public description: string
){}

}

这里我的基本功能在这里没什么奇怪的!

createNotification(notification : NotificationModel) {  
    return this._http.post('http://{myurl}/notifications', JSON.stringify(notification))
        .map(res => res.json());
}

updateNotification(notification : NotificationModel) {
    return this._http.put('http://{myurl}/notifications/' + notification.id, JSON.stringify(notification))
        .map(res => res.json());
}

当我尝试传递简单的测试值时,我得到以下内容: -

create为id生成9个字符的字母数字值,而在其他字段中不生成任何内容。 这是我的目标: -

{"id":5,"name":"NEW NOTIFICATION","description":"NEW NOTIFICATION DESCRIPTION"}

更新: - 这就是它创造的内容

{
  "id": "rkxCLjZLx"
}

更新消隐了其他两个字段,只保留id字段。 这是我的对象

{"id":"3","name":"Notification 3","description":"UPDATE DESCRIPTION"}

更新: - 此处是更新后的记录

{
  "id": "3"
}

这是一个json-server问题,还是有一些显而易见的错误?

更新

这是我用来调用我的服务的功能

addNotification(notification : NotificationModel) {
    this._notificationService.createNotification(new NotificationModel(5,
                                    'NEW NOTIFICATION', 'NEW NOTIFICATION DESCRIPTION')).subscribe(
         data => {
           // refresh the list
           this._notificationService.getNotifications().subscribe(res => {
                    this.notifications = res;
                }
            );
           return true;
         },
         error => {
           console.error("Error adding notification!");
           return Observable.throw(error);
         }
      );
}

updateNotification(notification : NotificationModel) {

    notification.description = 'UPDATE DESCRIPTION';

    this._notificationService.updateNotification(notification).subscribe(
         data => {
           // refresh the list
           this._notificationService.getNotifications().subscribe(res => {
                    this.notifications = res;
                }
            );
           return true;
         },
         error => {
           console.error("Error updating notification!");
           return Observable.throw(error);
         }
      );
}

1 个答案:

答案 0 :(得分:0)

找到了我的问题!

我没有在任何地方设置内容类型!通过更新我的PUT和POST请求,它现在可以按预期工作。

createNotification(notification : NotificationModel) {

    let body = JSON.stringify(notification);
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });         

    return this._http.post(this.serviceURL, body, options)
        .map(res => res.json());
}

updateNotification(notification : NotificationModel) {

    let body = JSON.stringify(notification);
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers }); 

    return this._http.put(this.serviceURL + '/' + notification.id, body, options)
        .map(res => res.json());
}