Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 创建和更新JSON调用不起作用_Angularjs_Json_Json Server - Fatal编程技术网

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

Angularjs 创建和更新JSON调用不起作用,angularjs,json,json-server,Angularjs,Json,Json Server,我正在学习Angular 2,有一个有趣的问题。我使用json服务器模拟我的服务调用,检索工作正常。但是,我在创建和更新方面有问题 我使用的是一个名为notification的基本模型,看起来是这样的:- export class NotificationModel { constructor( public id: number, public name: string, public description: string ){} } 这是我的基本功能,没什么奇怪的

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

我使用的是一个名为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服务器问题还是有明显的错误

更新

以下是我用来调用服务的函数

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);
         }
      );
}
找到我的问题了

我没有在任何地方设置内容类型!通过像这样更新我的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());
}

你能在创建新通知的地方发布代码吗?另外,您能否澄清测试值,我不清楚预期值与实际值是什么。在我的问题中添加了更多信息您浏览器的开发人员工具中的“网络”选项卡为http请求显示了什么?