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 g开发者_Go百科ets 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
From api.jquery.com
Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.
thanks for all the help folks. turns out I needed to add in JsonRequestBehavior.AllowGet
to my return statement
return Json(items); => return Json(items, JsonRequestBehavior.AllowGet);
*sigh
精彩评论