jQuery 通过 GET 将 JSON 数组传递给 MVC Web API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14628576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:47:51  来源:igfitidea点击:

Passing an JSON array to MVC Web API via GET

jqueryasp.net-mvcjsonasp.net-web-api

提问by Remy

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:

网址:

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 吗?

回答by Darin Dimitrov

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,-part-1

回答by Nav

**Following are two parameter Enum and objSearch **

**以下是两个参数 Enum 和 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) {

公共 IHttpActionResult 获取([FromUri]string 枚举,[FromUri]string objSearch){

IHttpActionResult action=null; return action;

IHttpActionResult action=null; 返回动作;

}

}