2017-06-03 52 views
0

試圖通過Angular http post方法來擊中WEB API,但無法擊中網頁api和catch塊也不會引發任何錯誤。我也啓用了跨國策略。 當我在瀏覽器中粘貼web api url時,它會觸發該方法,但不會通過角度http發佈功能。無法打網絡api。甚至無法獲得錯誤

下面是代碼組件代碼

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

    this._http.post(this._commonUrl, filter, options) 
     .map(response => response.json()) 
     .catch(this.handleError); 


    private handleError(error: Response) { 
     console.error(error); 
     return Observable.throw(error.json().error || 'Server error'); 
    } 

控制器代碼

[RoutePrefix("api/common")] 
public class ProductController : ApiController 
{ 
    public ProductController() 
    { 

    } 

    [Route("initialize")] 
    [HttpPost] 
    public HttpResponseMessage SearchLazada(Filter filter) 
    { 
     try 
     { 
     } 
    } 
} 
+0

的[角2 HTTP GET得不到]可能的複製(https://stackoverflow.com/questions/ 41381200/angular-2-http-get-not-getting) – Alex

回答

1

您的帖子請求將不會被提升。查看網絡電話。

  • 您需要訂閱它。

    this._http.post(this._commonUrl, filter, options) 
        .map(response => response.json()) 
        .catch(this.handleError) 
        .subscribe(data =>console.log(data)); 
    
  • 您可以使用錯誤回調訪問錯誤

    this._http.post(this._commonUrl, filter, options) 
        .map(response => response.json()) 
        .catch(this.handleError) 
        .subscribe(data =>console.log(data), 
        (error)=>console.log(error));////////////////////////// 
    
0

隨着內容類型:應用程序/ JSON,你應該變換體數據JSON。

篩選器:JSON.stringify(數據)

+0

噢,是的,我已經在上面的步驟中做了這個操作,我沒有在問題中粘貼... – user728630