后端,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);
}
}
打字稿功能:
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]
属性。结果每次都一样。