我正在使用 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/