如何使用 JavaScript Fetch 向带有字符串参数的 jsonresult 发送获取请求?

How to send a get request to a jsonresult with a string parameter using JavaScript Fetch?

我正在尝试从服务器获取 api。我正在使用 mvc jsonresult 创建 api。 jsonresult 将字符串 id 作为参数。那么如何使用 fetch 将 id 从客户端传递到服务器?

我尝试像发送 int 或任何数值一样发送 id,但没有成功!我也尝试将它作为查询字符串传递,但它也没有用!

这是我第一次尝试传递 id 的方式:

fetch(`/controller/jsonresult/${id}`)

我还尝试将其作为查询字符串获取

fetch(`/controller/jsonresult/get?id=${id}`)

我预计字符串会达到 jsonresult 所以我可以根据 id 获取信息但它总是收到 null

我刚才想出来了,但是忘记了 post 抱歉!

我在我的控制器中使用了 [Authorize] 数据注释,这就是我无法从我的控制器获取 jsonresult 的原因!

它工作得很好,

fetch(`/controller/jsonresult/${id}`)