jquery - 在 MVC 4 中将多个参数传递给操作时,困惑是使用 GET 还是 POST

标签 jquery post get asp.net-mvc-4 asp.net-web-api

我正在使用 Jquery 和 Jqgrid 开发 MVC 4 Web Api,到目前为止,我正在将多个数据发布到我的后 Controller 操作。

我的操作如下所示...

[ActionName("FetchProducts")]
public List<ABC> PostProducts(Product model)
{
    return _service.GetSomething(model);
}

public class Product{
    public string Name {get;set;}
    public string Category {get;set;}
    //.... and alteast 5 more properties
}

我的 jquery 调用类似于下面的......

$.ajax({  
type: "POST",  
url: /api/FetchProduct,  
data: this.getData(),  
contentType: "application/json; charset=utf-8",
dataType: "json",
success: callback
});

function getData(){
    return JSON.stringify({
            Name: "from somewhere" ,
            Category: "from somewhere",
            Price: "from somewhere",
            ABC: "from somewhere",
            XYZ: "from somewhere",
            //... and many more....
        }); 
}

这有效!但我工作的 friend 说

I am actually only getting data and should use 'GET' and not 'POST'. As GET is used to retrieve remote data, and POST is used to insert/update remote data.

我也觉得他是对的。那么我应该如何使用“GET”来做到这一点?

我是否必须将所有这些参数(至少有 10 个)作为查询字符串传递?

例如:api/FetchProduct/?Name='aaa'&&Category='vvv'&&.........

所以我的问题是在这种情况下应该做什么?我想知道其他开发者对此有何看法。谢谢

最佳答案

随着查询字符串的增长,您将遇到 URL 最大长度的问题,这取决于浏览器。

如果您有很多参数需要传递,那么即使您实际上只是获取数据,POST 也是完全可以接受的。然而,您将失去的是用户使用查询字符串为特定页面添加书签的能力。

绝对不应该使用 GET 的一种情况是,如果您传递的任何参数都是敏感的。

关于jquery - 在 MVC 4 中将多个参数传递给操作时,困惑是使用 GET 还是 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935720/

相关文章:

jQuery if css = value then

jquery - 谷歌地图信息窗口在 fancybox 中无法正确显示

ios - 如何在 swift 中为 Post 请求创建 JSON Body

java - HTTP Post 方法,我们可以提交的最大字节/数据?

java - 具有非 key 对的 Android 发布请求

javascript - 允许 div 随页面滚动而滚动,但仅在父 div 内

jquery - 设置 struts 2 自动完成器的默认值

testing - 如何为 Salesforce 进行 MultiMock Http 标注测试?

java - 为什么 Apache CXF - Jax RS 允许 'GET' 请求占用 'HEAD' 请求?

java - 从 ws.url 获取空指针异常