-1
私は角度2アプリからウェブAPIアプリケーションにデータを投稿しようとしています。その唯一の小さなJSON文字列と私はWeb APIのコントローラを押すと、アイテムは常にnullです - 私が何を試しても問題はありません。Web apiへのHttpPostは常にnull paraterです
JSONは
createUser(user: Profile): Observable<Response> {
let url = this.usersUrl;
let data = JSON.stringify(user);
let headers = new Headers({'Content-Type': 'application/json; charset=utf-8' });
let options = new RequestOptions({ headers: headers });
return this.http.post(url, data, options);
}
ウェブのAPI conroller方法がある
{"firstname":"Joe","lastname":"Bloggs"}
角度の関数である(私はデータを警告からこれを見ることができる)である。
[HttpPost]
public IActionResult Create(User item)
{
if (item == null)
{
return BadRequest();
}
Users.Add(item);
return CreatedAtRoute("GetUser", new { id = item.Id }, item);
}
しかし作成時にポイントを破ると、ユーザーアイテムがnullになります。
私が試した:
public IActionResult Create([FromBody] User item)
が、クロームコンソールウィンドウにエラーメッセージが私は
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' });
を試みたが、エラーが同じである
Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)
です上記のように。
任意のアイデアは?:)
ここではJSON.stringify()の代わりにJSON.parse()を使用してください。 let data = JSON.stringify(user);もしあなたがjsonの文字列を持っていれば、他のものを使ってはいけません。 – rashfmnb
work.Userはfirstname、lastnameプロパティを持つクラスです。エラーメッセージ - タイプをコンパイルしません。タイプstringのパラメーターにプロファイルを割り当てることはできません。 – thegunner
コントローラーメソッドpublic IHttpActionResult Create(JObjectユーザー) – rashfmnb