控制器未返回 json 结果以供查看
controller not returning json result to view
我正在尝试通过使用 jquery 来侦听更改来更新复选框列表。出于某种原因,控制器方法似乎没有 returning JSON。我将此代码基于 this example。
在示例中,控制器调用 return Json(data, JsonRequestBehavior.AllowGet);
,但我只能让 return Json(data)
工作。我的理论是 return 类型必须明确允许 GET,但我不知道如何让它工作。
我的代码工作正常,没有错误,所有 3 个控制台语句都出现在 Web 浏览器中。
我的 jquery 代码(在我看来):
$('#lgcheese').change(function() {
ddToPopulate.empty();
console.log('change detected');
$.getJSON(CoIdUrl, { CoName: $(this).val() }, function (data) {
console.log('in shit');
if (!data) { console.log('no json returned');return;}
ddToPopulate.append($('<ListItem></ListItem>').val('').text('Please select'));
$.each(data, function(index, item) {
ddToPopulate.append($('<ListItem></ListItem>').val(item.Value).text(item.Text));
});
});
});
稍后在我看来,我调用:<CheckBoxList ID="cheesebox"></CheckBoxList>
来创建 jquery 应该填充的复选框列表。
我的控制器有方法:
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
[HttpGet]
public JsonResult FetchCoIds(string CoName)
{
var data = new { Value = "val", Text = CoName+"_val1" };
return Json(data);
}
您需要用行为更新您的操作方法。试试这个
[HttpGet]
public JsonResult FetchCoIds(string CoName)
{
var data = new { Value = "val", Text = CoName+"_val1" };
return Json(data, JsonRequestBehavior.AllowGet);
}
注意:此答案适用于 asp.net MVC,不适用于 Core
我正在尝试通过使用 jquery 来侦听更改来更新复选框列表。出于某种原因,控制器方法似乎没有 returning JSON。我将此代码基于 this example。
在示例中,控制器调用 return Json(data, JsonRequestBehavior.AllowGet);
,但我只能让 return Json(data)
工作。我的理论是 return 类型必须明确允许 GET,但我不知道如何让它工作。
我的代码工作正常,没有错误,所有 3 个控制台语句都出现在 Web 浏览器中。
我的 jquery 代码(在我看来):
$('#lgcheese').change(function() {
ddToPopulate.empty();
console.log('change detected');
$.getJSON(CoIdUrl, { CoName: $(this).val() }, function (data) {
console.log('in shit');
if (!data) { console.log('no json returned');return;}
ddToPopulate.append($('<ListItem></ListItem>').val('').text('Please select'));
$.each(data, function(index, item) {
ddToPopulate.append($('<ListItem></ListItem>').val(item.Value).text(item.Text));
});
});
});
稍后在我看来,我调用:<CheckBoxList ID="cheesebox"></CheckBoxList>
来创建 jquery 应该填充的复选框列表。
我的控制器有方法:
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
[HttpGet]
public JsonResult FetchCoIds(string CoName)
{
var data = new { Value = "val", Text = CoName+"_val1" };
return Json(data);
}
您需要用行为更新您的操作方法。试试这个
[HttpGet]
public JsonResult FetchCoIds(string CoName)
{
var data = new { Value = "val", Text = CoName+"_val1" };
return Json(data, JsonRequestBehavior.AllowGet);
}
注意:此答案适用于 asp.net MVC,不适用于 Core