繁体   English   中英

如何在Angular get请求中将字符串值发送到后端

[英]How To Send String Value To Back end in an Angular get request

我期望在URL后面附加一个股票名称即可获得网络核心。 这是代码:

[Route("api/[controller]")]
[ApiController]
public class StockController : ControllerBase
{
    private static readonly Random generator = new Random();

    // GET api/stock/aapl
    [HttpGet("{r}")]
    public ActionResult<float[]> Get(
        string r,

我不确定Angular get中的格式。 这就是我所拥有的:

  getStock(name: string) {
// url should be api/stock/aapl
console.log("this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name});", this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name}))

return this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name});
}

后端和前端都可以编译。 console.log语句将其发送到console,格式不正确:

console.log("this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name});", this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name}))

我应该使用什么格式?

您似乎缺少了一个斜线。 如果您使用占位符而不是内联串联,那么代码将更具可读性

const url = `${this.apiURL}/api/stock/${name}`;
return this.http.get<Cagr[]>(url);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM