Angular2 Http post request not binding to ASP.NET 5 controller's action

后端 未结 3 1139
醉梦人生
醉梦人生 2020-12-21 08:31

I am initiating a post request from Angular2 to a ASP.NET 5 controller action. Angular is posting the data correctly and hitting the controller action but it is not being ma

3条回答
  •  甜味超标
    2020-12-21 09:09

    You try to send a JSON content (created using the JSON.stringify method) with a content type url encoded form.

    You should try to use the application/json one:

    let body = JSON.stringify({ firstName: 'Ali' });
    let headers = new Headers({ 'Content-Type': 'application/json' });
    
    this.http.post(this.url, body, { headers: headers })
    

    Edit

    If you want to provide a form content, you could leverage the URLSearchParams class:

    var params = new URLSearchParams();
    params.set('firstName', 'Ali');
    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    
    this.http.post(this.url, params.toString(), { headers: headers })
    

提交回复
热议问题