[英]Json data coming back null
Good evening, guys! 大家晚上好! I've got an odd one. 我有一个奇怪的。
Long story short, I'm sending a post from a java script client-side to get an integer back from my C# Controller, but the response.data is coming back null. 长话短说,我正在从Java脚本客户端发送帖子,以从C#控制器取回整数,但是response.data返回为null。 The twist is that both the C# method and the javascript/jquery function are literally copied and pasted from another project where both of them work. 不同的是,C#方法和javascript / jquery函数都是从另一个都可以正常工作的项目中复制并粘贴的。 The project they're taken from is a VS2010 project, and they're pasted into a VS2012 project. 他们从中获得的项目是一个VS2010项目,并将它们粘贴到VS2012项目中。 I'm not sure if this is the issue, but it could be related. 我不确定这是否是问题,但可能是相关的。 The integer is fetched correctly in the C# and none of the information is missing. 在C#中正确获取了整数,并且没有任何信息丢失。 Even more mysteriously, the success message is coming back correctly to the response object client-side. 更神秘的是,成功消息正确地返回到响应对象客户端。 However the response.data object is null, and throws an exception. 但是response.data对象为null,并引发异常。
Any and all help is very much appreciated. 任何帮助都将不胜感激。 Thanks! 谢谢!
This is the method in the C#: 这是C#中的方法:
[HttpPost]
public JsonResult GetMaxFileSize()
{
int MaxFileSize = 0;
// Get max file size.
string MaxPatientFileSizeInMegsString = System.Web.Configuration.WebConfigurationManager.AppSettings["MaxFacilityLogoFileSizeInMegs"];
MaxFileSize = int.Parse(MaxPatientFileSizeInMegsString);
return Json(new AjaxResponse(true, "Success.", new { maxFileSize = MaxFileSize }));
}
And this is the javascript/jquery function: 这是javascript / jquery函数:
function getMaxFileSize() {
$.post(settings.actions.getMaxFileSize, function (response) {
var maxFileSize = 0;
// Assign the correct size to the hidden field.
if (response.success) {
maxFileSize = response.data.maxFileSize;
$(settings.selectors.maxFileSizeHiddenInput).val(maxFileSize);
}
// Assign 0 to max file size: user cannot upload files.
else {
$(settings.selectors.maxFileSizeHiddenInput).val(maxFileSize);
}
});
}
Works fine here! 在这里工作正常!
TestController.cs TestController.cs
public class AjaxResponse
{
public AjaxResponse(bool success, object data)
{
this.success = success;
this.data = data;
}
public bool success { get; set; }
public object data { get; set; }
}
[HttpPost]
public ActionResult Ajax()
{
return Json(new AjaxResponse(true, new { num = 5 }));
}
Index.cshtml Index.cshtml
$.post('@Url.Action("Ajax", "Test")', function (response) {
var num = 0;
debugger;
// Assign the correct size to the hidden field.
if (response.success) {
num = response.data.num;
$('h2').html(num);
}
// Assign 0 to max file size: user cannot upload files.
else {
$('h2').html(num);
}
});
JSON response in FireBug: FireBug中的JSON响应:
{"success":true,"data":{"num":5}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.