admin管理员组文章数量:1401632
I want to send a request to a C# service which has a string as its parameter. I wrote an angular service like this:
export class CategoryService {
constructor(private http: HttpClient) { }
getCategory(categoryName: string): Observable<Category> {
console.log(categoryName);
return this.http.post("/category/GetCategory", { categoryName: categoryName }, { headers: new HttpHeaders({ 'Content-type': 'application/json' }) });
}
I also tried:
export class CategoryService {
constructor(private http: HttpClient) { }
getCategory(categoryName: string): Observable<Category> {
console.log(categoryName);
return this.http.post("/category/GetCategory", categoryName , { headers: new HttpHeaders({ 'Content-type': 'application/json' }) });
}
But when they both successfully send the request, the parameter is always null. My C# service is:
[HttpPost]
public IActionResult GetCategory([FromBody]string categoryName)
I want to send a request to a C# service which has a string as its parameter. I wrote an angular service like this:
export class CategoryService {
constructor(private http: HttpClient) { }
getCategory(categoryName: string): Observable<Category> {
console.log(categoryName);
return this.http.post("/category/GetCategory", { categoryName: categoryName }, { headers: new HttpHeaders({ 'Content-type': 'application/json' }) });
}
I also tried:
export class CategoryService {
constructor(private http: HttpClient) { }
getCategory(categoryName: string): Observable<Category> {
console.log(categoryName);
return this.http.post("/category/GetCategory", categoryName , { headers: new HttpHeaders({ 'Content-type': 'application/json' }) });
}
But when they both successfully send the request, the parameter is always null. My C# service is:
[HttpPost]
public IActionResult GetCategory([FromBody]string categoryName)
Share
Improve this question
asked Nov 24, 2018 at 10:20
Nick Mehrdad BabakiNick Mehrdad Babaki
12.6k15 gold badges50 silver badges72 bronze badges
8
- 1 The first approach is the correct approach, the second one is not. But as you said both of them don't work. In that case could you check if you are able to successfully receive the categoryName when you send it via Postman for eg? – SiddAjmera Commented Nov 24, 2018 at 10:22
- If I remove [FromBody] I can receive it by Postman but with [FromBody] I cannot – Nick Mehrdad Babaki Commented Nov 24, 2018 at 10:32
-
I'm not so sure about your C# service. I don't even know why [FromBody] is used, but if you're getting it from Postman after removing
[FromBody]
, then with the first approach that you tried, you should also get it from Angular. – SiddAjmera Commented Nov 24, 2018 at 10:34 - I go to the service but the parameter is always null. – Nick Mehrdad Babaki Commented Nov 24, 2018 at 10:35
-
2
have you tried using
JSON.stringify(categoryName)
in your typescript service withFromBody
in controller? – Niladri Commented Nov 24, 2018 at 10:59
4 Answers
Reset to default 2I can offer my own solution :)
Angular:
Func(data: string): Observable<any> {
let params = new HttpParams();
params = params.set('data', data);
return this.http.post("url", params);
}
C#:
public IActionResult Method([FromForm]string data)
Best and easiest solution
this.http.post(this.serverAddress + API_ADDRESS + '?param=' + yourStringParam, null);
I found the solution here: Angular2 - Http POST request parameters I also should remove [FromBody] from my service parameter.
As @Niladri said, this worked for me:
... using JSON.stringify(categoryName) in your typescript service with FromBody in controller
Also this is important:
... keep the content-type as HttpHeaders({ 'Content-Type': 'application/json' })
It works with the FromBody in the controller method :)
本文标签: javascriptAngular HttpClient post cannot send simple string parameterStack Overflow
版权声明:本文标题:javascript - Angular HttpClient post cannot send simple string parameter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744317724a2600344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论