Angular2 http发布请求错误为{" isTrusted":true}

时间:2017-06-22 07:12:35

标签: angular2-forms

我在将数据从An2发布到WebApi

时收到此错误

Service.ts

    AddEmp(Url2: string, model): Observable<any> {
let body = JSON.stringify(model);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });
        return this._http.post(this.Url2, body, options)
            .map((response: Response) => <any>response.json())
            .catch(this.handleError);
    }

Component.ts

 Employees: Employee[] = [];
    submit(EmpData): any{
            this._HttpService.AddEmp(this.Url2, EmpData)
                .subscribe(data => {
                    EmpData = data, error => console.log(), () => alert('ok...')
                });    
            }

1 个答案:

答案 0 :(得分:0)

我现在遇到了这个问题。
原因是事件命名。

问题:

将@Output命名为// In emitter <form (ngSubmit)="onSubmit($event);" > ... @Output submit = new EventEmitter<any>(); ... onSubmit(event) { this.submit.emit(this.formGroup.value); // Event('submit') triggered } // In subscriber: (submit)="onSave($event)" onSave(data) { // There are 2 events triggered. // - Our manually triggered form data // - Original submit Event with public {isTrusted: true} property. this.http.post(uri, data); // So, we call two consequent POST query } 时,还会收到默认的传播事件。

示例:

onSubmit(event) { event.stopPropagation(); ...

解决方案:

  • 停止原始事件传播:

    submitData

  • 将自定义事件重命名为library(zoo) rollapply(1:ncol(anscombe), 4, by = 2, function(ix) sum(anscombe[, ix]))