我正在使用jQuery的自动完成功能.当我尝试检索超过17000条记录的列表(每条记录的长度不超过10条)时,它超出了长度并抛出错误:
异常信息:
异常类型:InvalidOperationException
异常消息:使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过maxJsonLength属性上设置的值.
我可以设置无限长度maxJsonLength的web.config?如果没有,我可以设置的最大长度是多少?
我正在尝试使用jQuery的ajax()函数将一个对象数组传递给MVC控制器方法.当我进入PassThing()C#控制器方法时,参数"things"为null.我已经尝试使用一种List作为参数,但这也不起作用.我究竟做错了什么?
<script type="text/javascript">
$(document).ready(function () {
var things = [
{ id: 1, color: 'yellow' },
{ id: 2, color: 'blue' },
{ id: 3, color: 'red' }
];
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '/Xhr/ThingController/PassThing',
data: JSON.stringify(things)
});
});
</script>
public class ThingController : Controller
{
public void PassThing(Thing[] things)
{
// do stuff with things here...
}
public class Thing
{
public int id { get; set; }
public string color { get; set; } …Run Code Online (Sandbox Code Playgroud) 我整个下午都试图在网上爬行,试图在动作控制器中接收一个JSON对象.
这样做的正确和/或更简单的方法是什么?
我尝试过以下方法:1:
//Post/ Roles/AddUser
[HttpPost]
public ActionResult AddUser(String model)
{
if(model != null)
{
return Json("Success");
}else
{
return Json("An Error Has occoured");
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我输入的空值.
2:
//Post/ Roles/AddUser
[HttpPost]
public ActionResult AddUser(IDictionary<string, object> model)
{
if(model != null)
{
return Json("Success");
}else
{
return Json("An Error Has occoured");
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我jquery方面的500错误,试图发布到它?(意思是它没有正确绑定).
这是我的jQuery代码:
<script>
function submitForm() {
var usersRoles = new Array;
jQuery("#dualSelectRoles2 option").each(function () {
usersRoles.push(jQuery(this).val());
});
console.log(usersRoles);
jQuery.ajax({
type: "POST",
url: "@Url.Action("AddUser")",
contentType: "application/json; charset=utf-8",
dataType: "json", …Run Code Online (Sandbox Code Playgroud) 我正在尝试以下内容:带有字典的模型将其发送到第一个ajax请求,然后将结果再次序列化并将其发送回控制器.
这应该测试我可以在模型中找回字典.它不起作用
这是我的简单测试:
public class HomeController : Controller
{
public ActionResult Index (T a)
{
return View();
}
public JsonResult A(T t)
{
if (t.Name.IsEmpty())
{
t = new T();
t.Name = "myname";
t.D = new Dictionary<string, string>();
t.D.Add("a", "a");
t.D.Add("b", "b");
t.D.Add("c", "c");
}
return Json(t);
}
}
//model
public class T
{
public string Name { get; set; }
public IDictionary<string,string> D { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
javascript:
$(function () {
var o = {
Name: 'somename',
"D": …Run Code Online (Sandbox Code Playgroud) 我创建了一个测试网站来调试我遇到的问题,看起来我要么传递JSON数据错误,要么MVC只能绑定可空的longs.当然,我正在使用最新的MVC 3版本.
public class GetDataModel
{
public string TestString { get; set; }
public long? TestLong { get; set; }
public int? TestInt { get; set; }
}
[HttpPost]
public ActionResult GetData(GetDataModel model)
{
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
我发布了一个具有正确JSON内容类型的JSON字符串:
{ "TestString":"test", "TestLong":12345, "TestInt":123 }
Run Code Online (Sandbox Code Playgroud)
长期没有约束,它总是空的.如果我把这个值放在引号中,它就可以工作,但我不应该这样做,不是吗?我需要为该值设置自定义模型绑定器吗?
我正在将数据发布到MVC控制器,我正在尝试维护状态以及乐观并发.我目前正在回复一个JSON请求,但是对于可行的替代方案是开放的吗?
我已经使用以下命令发布了一个名称/值集合:
$.ajax({
url: g_appPath + "/Rounding.aspx/Round/" + $("#OfferId").val(),
type: 'POST',
dataType: 'html',
data: $.toJSON(data), // <-- data = name/value array
contentType: 'application/json; charset=utf-8',
beforeSend: doSubmitBeforeSend,
complete: doSubmitComplete,
success: doSubmitSuccess
});
Run Code Online (Sandbox Code Playgroud)
我还有一个(加密的)id和时间戳数组,我希望将其传回,以便服务器可以对其进行解密,然后在保存数据之前验证数据是否仍然新鲜.
非常重要的是,数据对象是独立的,并且不是一个或另一个的子节点或在包装器数组中(因为在服务器端反射反序列化).同样重要的是要注意我想要异步执行此操作而不是表单提交.
我的问题是:有什么办法可以使用'application/json'作为内容类型回发2个JSON对象吗?
我的另一个问题是:有没有更好的/另一种方式我可以做到这一点?
提前致谢!
更新:我解决了我的问题,通过将contentType参数更改为默认值,而是将字符串化的ajax数据作为单独的命名参数发送到查询字符串中.
当你使用contentType:'application/json; charset = utf-8',这会将数据推送到请求的主体,而不是查询字符串.我的新$ .ajax()帖子现在看起来像这样:
$.ajax({
url: g_appPath + "/Rounding.aspx/Round/" + $("#OfferId").val(),
type: 'POST',
dataType: 'html',
data: "RoundingData=" + $.toJSON(data) + "&StateData=" + $.toJSON(stateData),
// --removed! contentType: 'application/json; charset=utf-8',
beforeSend: doSubmitBeforeSend,
complete: doSubmitComplete,
success: doSubmitSuccess
});
Run Code Online (Sandbox Code Playgroud)
这个问题真的出现了,因为我对这种类型的数据操作缺乏经验,我希望将来有人能够发现这个问题.
谢谢!
担
我正在尝试使用 Newtonsoft.json 将 json 字符串转换为对象,但以下转换遇到一些问题。我想知道是否有人可以解释这一点。谢谢。
AddFaceResponse ir = JsonConvert.DeserializeObject<AddFaceResponse>(responseContentStr);
Run Code Online (Sandbox Code Playgroud)
这是 json 字符串 responseContentStr
[{
"faceId": "1fe48282-a3b0-47d1-8fa8-67c4fac3d984",
"faceRectangle": {
"top": 80,
"left": 50,
"width": 147,
"height": 147
}
}]
Run Code Online (Sandbox Code Playgroud)
这是我的模型对象。
public class AddFaceResponse
{
public class Face
{
public class FaceRectangle
{
public int top, left, width, height;
public FaceRectangle(int t, int l, int w, int h)
{
top = t;
left = l;
width = w;
height = h;
}
}
public string faceId;
public FaceRectangle faceRectangle;
public Face(string id, …Run Code Online (Sandbox Code Playgroud) json ×5
asp.net-mvc ×4
c# ×4
asp.net ×2
jquery ×2
actionmethod ×1
ajax ×1
dictionary ×1
json.net ×1
nullable ×1
post ×1