JSON 回调未被调用
以下代码在 v1.3 中有效,但在 1.4 中无效,我似乎无法弄清楚问题是什么。我正在从省份 ddl 中选择构建一个城市 ddl。我正在使用 ASP.NET MVC 2
服务器上的函数被调用并构建正确的列表,但当它返回时,它永远不会进入回调函数。我已经设置了一些警报,但没有接到电话。除了 getJSON 之前的那个
有人知道我做错了什么吗?
$(函数(){
var provinces = $("#ProvinceId");
var cities = $("#CityId");
provinces.change(function () {
cities.find('option').remove();
alert("hello outside JSON call");
$.getJSON('<%= Url.Content("~/HomeController/Cities") %>', { province: provinces.val(), includeAllPlaceholder : true }, function (data) {
alert("hello");
$(data).each(function () {
alert("hello 2");
$("<option value=" + this.Value + ">" + this.Text + "</option>").appendTo(cities);
alert(this.Value + ":" + this.Text);
});
});
});
});
在控制器中
public JsonResult Cities(string province, bool includeAllPlaceholder)
{
List<SelectListItem> items = new List<SelectListItem>();
int provinceId;
if (int.TryParse(province, out provinceId))
{
var values = ReferenceTableService.CitiesInProvince(provinceId).Where(f => includeAllPlaceholder || (includeAllPlaceholder == false && f.IsAllPlaceholderEntry == false)).ToList();
values.Sort();
items.Add(new SelectListItem { Value = "", Text = Resources.Global.Generic.ddlSelectValue });
items.AddRange(values.Select(f => new SelectListItem { Value = f.Id.ToString(), Text = f.Name }));
}
return Json(items);
}
感谢
TR
The following code has worked in v1.3 but not 1.4 I can't seem to figure out what the issue is. I'm building a cities ddl from a selection from the province ddl. I'm using ASP.NET MVC 2
The function on the server gets called and it builds up the correct list, but when it returns it never steps into the callback function. I've put in some alerts, and the don't get called. except for the one before the getJSON
Anyone know what I'm doing wrong?
$(function () {
var provinces = $("#ProvinceId");
var cities = $("#CityId");
provinces.change(function () {
cities.find('option').remove();
alert("hello outside JSON call");
$.getJSON('<%= Url.Content("~/HomeController/Cities") %>', { province: provinces.val(), includeAllPlaceholder : true }, function (data) {
alert("hello");
$(data).each(function () {
alert("hello 2");
$("<option value=" + this.Value + ">" + this.Text + "</option>").appendTo(cities);
alert(this.Value + ":" + this.Text);
});
});
});
});
in the controller
public JsonResult Cities(string province, bool includeAllPlaceholder)
{
List<SelectListItem> items = new List<SelectListItem>();
int provinceId;
if (int.TryParse(province, out provinceId))
{
var values = ReferenceTableService.CitiesInProvince(provinceId).Where(f => includeAllPlaceholder || (includeAllPlaceholder == false && f.IsAllPlaceholderEntry == false)).ToList();
values.Sort();
items.Add(new SelectListItem { Value = "", Text = Resources.Global.Generic.ddlSelectValue });
items.AddRange(values.Select(f => new SelectListItem { Value = f.Id.ToString(), Text = f.Name }));
}
return Json(items);
}
thanks
TR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 api.jquery.com
From api.jquery.com
感谢大家的帮助。事实证明我需要将
JsonRequestBehavior.AllowGet
添加到我的返回语句中*叹息
thanks for all the help folks. turns out I needed to add in
JsonRequestBehavior.AllowGet
to my return statement*sigh