[英]How can i pass a parameters to action method using javascript fetch?
I'm using js fetch
to call to an action method(in asp.net mvc), but I need to pass parameters to the method.我正在使用 js
fetch
调用操作方法(在 asp.net mvc 中),但我需要将参数传递给该方法。
How can I pass this data using fetch
?如何使用
fetch
传递这些数据?
I'm trying to use this code but the data didn't pass:我正在尝试使用此代码,但数据没有通过:
fetch(url, {
data: { repositoryName: "dada" }
})
.then(() => {
console.log("test");
});
According to the documentation second parameter shouldn't contain data
property.根据文档,第二个参数不应包含
data
属性。
I guess you need to use body
property(you can use JSON string, FormData
or URLSearchParams
here basing on your needs, see docs ):我猜您需要使用
body
属性(您可以根据需要在此处使用 JSON 字符串、 FormData
或URLSearchParams
,请参阅文档):
fetch(url, {
method: "POST",
body: JSON.stringify({ repositoryName: "dada" })
}).then(() => { ... })
Also in this simple example it's possible to simply use query string like this:同样在这个简单的例子中,可以简单地使用这样的查询字符串:
fetch(url + '?repositoryName=dada').then(() => { ... })
Note that GET requests cannot have body so if your MVC action is GET action then you have to use query strings like in my second example.请注意,GET 请求不能有正文,因此如果您的 MVC 操作是 GET 操作,那么您必须使用我第二个示例中的查询字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.