Angular 2 - 在位置0的JSON中的意外标记 - WebAPI [HttpPost]

问题描述:

我想从角度2 http服务调用我的POST端点(ASP.NET WebAPI),但端点根本没有被击中。我已经阅读了几篇文章,但是他们都没有工作。Angular 2 - 在位置0的JSON中的意外标记 - WebAPI [HttpPost]

从我的角度2部分,我打电话给我的服务像

saveCarAuction(): void { 
    let respone: ResponseObject; 
    this.auctionservice.saveCarAuction(this.auction).subscribe(resp => { 
     respone = resp; 
    }, error => this.onError(error)); 
} 

在auctionservice,我有

saveCarAuction(auction: CarAuction): Observable<ResponseObject> { 
    let headers = new Headers({ 'Content-Type': 'application/json' }); 
    let options = new RequestOptions({ headers: headers }); 

    var api: string = '/CarAuction'; 
    var url: string = this.baseApiPath + api; 
    return this.http.post(url, JSON.stringify(auction), options) 
     .map((res: Response) => res.json()); 
} 

在我的Web API控制器,我有

[HttpPost] 
[Route("api/CarAuction")] 
[ActionName("PostCarAuction")] 
public IHttpActionResult PostCarAuction([FromBody]CarAuctionViewModel carAuction) 
{ 
    ResponseObject response = new ResponseObject() {ResponseType = ResponseType.Success.ToString()}; 
    //some action 
    return Ok(Utility.SerializeObject(response)); 
} 

我服务电话的最终网址是

http://localhost:53796/api/CarAuction 

问题是,如果我在我的操作中放置断点,它将不会被调用。我试图从我的服务不同的传递数据如下图所示:

JSON.stringify({ carAuction: auction}) 

我也试图通过属性路由调用它(虽然我不喜欢那个),但不工作或者。我试图在我的发布操作中接收简单的json字符串,并且反序列化并尝试更新返回类型,但似乎它已决定玩一个棘手的游戏。

在我的控制台中的所有请求(变化)的响应低于

enter image description here

我真的跑出去的想法,现在做成功运行这个简单的场景。

编辑1: 我已经包括了我传递给我的API

{ 
    "carAuction": { 
     "carPlateNumber": null, 
     "carAuctionId": null, 
     "sellerUserId": null, 
     "carMakeId": null, 
     "carModelId": null, 
     "carColorId": null, 
     "carType": null, 
     "manufacturerYear": 2002, 
     "manufacturerMonth": 4, 
     "carMileage": 475000, 
     "ownershipCount": 3, 
     "ownershipType": null, 
     "comments": null, 
     "notes": null, 
     "terms": null, 
     "coverPhotoUrl": null, 
     "salePublicationDateTime": null, 
     "saleEndDateTime": null, 
     "auctionType": null, 
     "carbidPercentage": null, 
     "saleType": null, 
     "isHiddenAuction": false, 
     "intervalBid": 0, 
     "minimumPrice": 0, 
     "contactPersonFullName": null, 
     "contactPersonCompany": null, 
     "contactPersonPhone": null, 
     "contactPersonFax": null, 
     "contactPersonWebsite": null, 
     "contactPersonEmail": null, 
     "contactPersonAddress": null, 
     "hasAuctionEnded": false, 
     "carLocation": null, 
     "carMake": null, 
     "carModel": null, 
     "carColor": null, 
     "carAuctionMedia": [], 
     "id": "3F8184F7-37F4-4BD9-8B32-FDB8F65D5AB3", 
     "appCultureId": null, 
     "createdDate": null, 
     "createdBy": null, 
     "isActive": true, 
     "modifiedDate": null, 
     "modifiedBy": null 
    } 
} 
+0

分享您的“拍卖”价值,您进入'saveCarAuction'功能? – ranakrunal9

+0

我已经包含使用以下格式传递给API的json:JSON.stringify({carAuction:auction}),它的有效JSON –

+0

@AliBaig什么是baseApiPath? – Milad

的JSON你应该使用network tab检查从服务器的响应并将它添加到你的问题。 看起来像你的JSON响应已损坏。当发送输出之前或没有发送任何内容时,通常会发生错误消息。

我的猜测是,您将所有请求重定向到index.html或您正在收到自定义404页面。尝试在您的浏览器中访问http://localhost:53796/api/CarAuction并检查响应。

意外标记<在JSON

这几乎总是指试图解析到JSON一个HTML页面。您发布的响应图片不是您从服务器获得的回复。你应该检查网络标签。