【发布时间】:2016-08-16 19:55:55
【问题描述】:
后端,ASP.net Core API:
[Produces("application/json")]
[Route("api/[controller]")]
public class StoriesController : Controller
{
public static List<Story> STORIES = new List<Story>
{
new Story
{
content = "Some really interesting story about dog",
timeOfAdding = new DateTime(2016, 8, 26),
numberOfViews = 11
},
new Story
{
content = "Even cooler story about clown",
timeOfAdding = new DateTime(2016, 9, 26),
numberOfViews = 11
},
new Story
{
content = "And some not cool story",
timeOfAdding = new DateTime(2016, 10, 26),
numberOfViews = 11
}
};
// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
Story story = new Story
{
content = value,
timeOfAdding = DateTime.Now,
numberOfViews = 0
};
STORIES.Add(story);
}
}
TypeScript 函数:
add(content: string): Observable<Story> {
let body = JSON.stringify({ "value": content });
//let body = { "value": content };
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers});
return this.http.post(this.heroesUrl, body, options)
.map(this.extractData)
.catch(this.handleError);
}
发送的参数(在 Firefox 控制台中看到):
value = null 在 Visual Studio 2015 调试器中
怎么了?我已经尝试了我在互联网上找到的所有内容:添加/删除标题、删除 JSON.stringify、添加/删除 [FromBody] 属性。结果每次都一样。
【问题讨论】:
标签: c# asp.net angularjs angular