Angular 4,一个PUT http请求给我一个错误

时间:2017-09-27 19:17:18

标签: angular typescript

我有一个发送一些JSON数据并接收JSON数据的方法。我收到以下错误:

SyntaxError: Unexpected token p in JSON at position 0
    at Object.parse (<anonymous>)
    at Response.webpackJsonp.../../../http/@angular/http.es5.js.Body.json (http.es5.js:797)
    at MapSubscriber.project (api.service.ts:172)
    at MapSubscriber.webpackJsonp.../../../../rxjs/operator/map.js.MapSubscriber._next (map.js:77)
    at MapSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)
    at XMLHttpRequest.onLoad (http.es5.js:1226)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:425)
    at Object.onInvokeTask (core.es5.js:3881)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (zone.js:192)
this.api.editProduct(this.PID, SCHEMA).subscribe( data => {                        
    this.mediator.openDynamicSnackBar(`Το προϊόν ενημερώθηκε`, 'success', 'ok');                           
    this.router.navigate([`/crm/admin/products`]);

}, error => {
  console.log(error);
  this.mediator.openDynamicSnackBar('Error while updating', 'error','ok');
});

api.service.ts

 /**
   * Edit the selected product, takes an id and the new object as parammeter
   * @param id
   * @param newProduct
   */
  editProduct(id: any, newProduct: any) {
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    const body = JSON.stringify(newProduct);

    return this.http.put(`${this.BASE_URL}/api/products/${id}`, body, { headers: headers })
      .map( res => res.json())
  }

0 个答案:

没有答案