を渡し、最近私がAngular2とAsp.netコアを学び始め、ここにオブジェクトを投稿するには問題に走った私のコードです:Angularjs2 Postメソッドは空のオブジェクト
Service.ts
ファイル:
export class SubCategoryService {
//private headers: Headers;
constructor(private http: Http) {
//this.headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
// this.headers.append('Content-Type', 'application/json');
// this.headers.append('Accept', 'application/json');
}
public createItem = (subCategory: SubCategory): Observable<SubCategory> =>
{
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let toAdd = JSON.stringify(subCategory);
return this.http.post("api/subcategory", toAdd, { headers: headers }).map((response: Response) => <SubCategory>response.json()).catch(this.handleError);
}
}
Component.ts
ファイル:
export class SubCategoryComponent {
constructor(private service: SubCategoryService) { }
subCategories: SubCategory[];
SubCategory: SubCategory = new SubCategory();
onPost() {
this.service.createItem(this.SubCategory).subscribe(
subCategory => this.subCategories.push(subCategory),
error => console.log(error),
() => console.log('Get all items completed'));
this.isLoading = true;
}
}
Asp.NetコアController
[HttpPost]
public async Task<JsonResult> Post(SubCategory subCategory)
{
return new JsonResult("");
}
これは空のobjecで私のコントローラに当たっています...助けていただければ幸いです。
また、郵便配達員と一緒に試してみるとうまくいきますが、体内の情報に何か問題がありますか?ここで
はそれが仕事をしているとスクリーンショットです:
requestb.inまたはこれらのようなエコーサービスを使用してみてください:http://stackoverflow.com/questions/5725430/http-test-server-that-accepts-get-post-calls – Jim
そしてI https://echo.getpostman.com/ – Jim