I know there are tons of answers for this topic, but couldn't find the solution to my issue. I have an ASP.NET MVC Web API that looks like this:
我知道这个问题有很多答案,但是我找不到解决这个问题的方法。我有一个ASP。NET MVC Web API如下:
[HttpGet]
public IList<Country> GetCountryList(List<long> idList)
And I've tried calling it like this:
我试着这样称呼它:
$.ajax({
dataType: "json",
data: JSON.stringify({idList: listOfIds}),
type: "GET",
url: "api/v1/util/CountryList",
success: function (result) {
alert(result);
}
});
The URL then looks like this:
URL是这样的:
https://localhost/supertext/api/v1/util/CountryList?{%22idList%22:[46,14,62,83,120]}
Alternative:
选择:
$.ajax({
dataType: "json",
data: {
idList: JSON.stringify(listOfIds),
}
type: "GET",
url: "api/v1/util/CountryList",
success: function (result) {
alert(result);
}
});
URL:
URL:
https://localhost/supertext/api/v1/util/CountryList?idList=%5B46%2C14%2C62%2C83%2C120%5D
Both methods don't work.
这两种方法都不工作。
Do I really have to send and receive it as a string or use POST?
我真的需要发送和接收它作为一个字符串还是使用POST?
2 个解决方案
#1
54
No, don't try to be sending JSON in a GET request. Use JSON with other verbs which have body, such as POST and PUT.
不,不要尝试在GET请求中发送JSON。使用JSON和其他有正文的动词,如POST和PUT。
Do it the standard way, by decorating your action parameter with the [FromUri]
attribute:
通过使用[FromUri]属性来修饰动作参数,按照标准方式进行:
public IList<Country> GetCountryList([FromUri] List<long> idList)
{
...
}
and then just trigger the AJAX request:
然后触发AJAX请求:
$.ajax({
url: 'api/v1/util/CountryList',
type: 'GET',
data: { idList: [1, 2, 3] },
traditional: true,
success: function (result) {
console.log(JSON.stringify(result));
}
});
Further recommended reading for you about how the model binding in the Web API works:
关于Web API中的模型绑定如何工作的进一步推荐阅读:
http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1
http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,第1部分
#2
0
**Following are two parameter Enum and objSearch **
下面是两个参数枚举和objSearch **。
var Enum = "ABCD";
var Enum =“ABCD”;
var objSearch = [
{"Name":"Navjot Angra","Age":23},
{"Name":"Nav","Age":22}];
//this is ajax method
/ /这是ajax方法
$.ajax({
. ajax({美元
type: "GET",
var GatwayUrl ='http//2937/' (//Your url)
url: GatwayUrl + 'api/Certificate/GetDetail/?Enum=' + Enum +'+&objSearch='+ JSON.stringify(objSearch),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result) {
alert("Your Code");
} }
});
//this part is web api part
//这部分是web api部分
[HttpGet]
(HttpGet)
public IHttpActionResult Fetch([FromUri]string Enum, [FromUri]string objSearch) {
public IHttpActionResult Fetch([FromUri]string Enum, [FromUri]string objSearch) {
IHttpActionResult action=null; return action;
IHttpActionResult行动=零;返回行动;
}
}
#1
54
No, don't try to be sending JSON in a GET request. Use JSON with other verbs which have body, such as POST and PUT.
不,不要尝试在GET请求中发送JSON。使用JSON和其他有正文的动词,如POST和PUT。
Do it the standard way, by decorating your action parameter with the [FromUri]
attribute:
通过使用[FromUri]属性来修饰动作参数,按照标准方式进行:
public IList<Country> GetCountryList([FromUri] List<long> idList)
{
...
}
and then just trigger the AJAX request:
然后触发AJAX请求:
$.ajax({
url: 'api/v1/util/CountryList',
type: 'GET',
data: { idList: [1, 2, 3] },
traditional: true,
success: function (result) {
console.log(JSON.stringify(result));
}
});
Further recommended reading for you about how the model binding in the Web API works:
关于Web API中的模型绑定如何工作的进一步推荐阅读:
http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1
http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,第1部分
#2
0
**Following are two parameter Enum and objSearch **
下面是两个参数枚举和objSearch **。
var Enum = "ABCD";
var Enum =“ABCD”;
var objSearch = [
{"Name":"Navjot Angra","Age":23},
{"Name":"Nav","Age":22}];
//this is ajax method
/ /这是ajax方法
$.ajax({
. ajax({美元
type: "GET",
var GatwayUrl ='http//2937/' (//Your url)
url: GatwayUrl + 'api/Certificate/GetDetail/?Enum=' + Enum +'+&objSearch='+ JSON.stringify(objSearch),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result) {
alert("Your Code");
} }
});
//this part is web api part
//这部分是web api部分
[HttpGet]
(HttpGet)
public IHttpActionResult Fetch([FromUri]string Enum, [FromUri]string objSearch) {
public IHttpActionResult Fetch([FromUri]string Enum, [FromUri]string objSearch) {
IHttpActionResult action=null; return action;
IHttpActionResult行动=零;返回行动;
}
}