簡體   English   中英

ajax無法從json獲得響應

[英]ajax not getting response from json

在MVC4的ASP.NET中,我有兩個級聯的下拉列表。 我正在使用ajax調用從Controller調用Json方法。 Json方法將返回一組我需要用於下拉列表的值,但它不在ajax中。 它總是顯示錯誤消息,無法加載數據。

以下是Ajax調用的代碼

 $("#countryname").change(function () {

        if ($("#countryname").val() != "Please select") {
            var options = {};
            options.url = "/ServeEasy/GetStates";
            options.type = "GET";
            //options.data = JSON.stringify({ countryID: $("#countryname").val() });
            options.data = { countryID: $("#countryname").val() };
            //alert($("#countryname").val());
            options.dataType = "json";
            options.contentType = "application/json";
            debugger;
            options.success = function (slist) {
                $("#statename").empty();
                for (var i = 0; i < slist.length; i++) {
                    $("#statename").append("<option>" + slist[i] + "</option>");
                }
                $("#statename").prop("disabled", false);
            };
            options.error = function () { alert("Error retrieving states!"); };
            $.ajax(options);
        }            
        else {
            $("#statename").empty();
            $("#statename").prop("disabled", true);
        }
    });

Json方法如下

[HttpGet]
    public JsonResult GetStates(string countryID)
    {
        List<SelectListItem> StateList = new List<SelectListItem>();
        IEnumerable<FOSEntities.State> slist = new List<FOSEntities.State>();
        slist = serveEasyServices.GetStateByCountry(countryID);
        return Json(slist, JsonRequestBehavior.AllowGet);
        //return Json(new { slist = slist }, JsonRequestBehavior.AllowGet);
    }

像這樣嘗試,這僅僅是示例

 $("#CustomerId").change(function () {
      var Id = $("#CustomerId").val();
      var options = {};
            options.url = '@Url.Action("GetCustomerNameWithId", "Test")';
            options.type = "GET";
            //options.data = JSON.stringify({ countryID: $("#countryname").val() });
            options.data = { CustomerNameId: Id };
            //alert($("#countryname").val());
            options.dataType = "json";
            options.contentType = "application/json";
            options.success = function (listItems) {
                $("#CustomerNameId").empty();
                for (var i = 0; i < listItems.length; i++) {
                    $("#CustomerNameId").append('<option value="' + listItems[i].Value + '">' + listItems[i].Text + '</option>');
                }
            };
            options.error = function () { alert("Error retrieving states!"); };
            $.ajax(options);
        });

   @Html.DropDownList("CustomerId", (SelectList)ViewBag.CustomerNameID, "--Select--")
   @Html.DropDownList("CustomerNameId", new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "-- Select --")

控制者

public JsonResult GetCustomerNameWithId(string CustomerNameId)
{
 int _CustomerNameId = 0;
 int.TryParse(CustomerNameId, out _CustomerNameId);
 var listItems = GetCustomerNameId(_CustomerNameId).Select(s => new SelectListItem { Value = s.CID.ToString(), Text = s.CustomerName }).ToList<SelectListItem>();
 return Json(listItems, JsonRequestBehavior.AllowGet);
}

 public List<Customer> GetCustomerNameId(int CustomerNameId)
 {
   // Customer cascade DropDown
   using (dataDataContext _context = new dataDataContext())
   {
    return (from c in _context.Customers
            where c.CID == CustomerNameId
            select c).ToList();
   }
  }

我做了以下更改,並成功了!!

關於Json方法,以下更改

public JsonResult GetStates(string countryID)
    {
        List<SelectListItem> StateList = new List<SelectListItem>();   
        var slist = serveEasyServices.GetStateByCountry(countryID).Select(s => new SelectListItem { Value = s.StateID.ToString(), Text = s.State }).ToList<SelectListItem>();
        return Json(slist, JsonRequestBehavior.AllowGet);
    }

然后在Ajax上

$("#countryname").change(function () {
        var Id = $("#countryname").val();
        if ($("#countryname").val() != "Please select") {
            var options = {};
            options.url = '@Url.Action("GetStates", "ServeEasy")'; 
            options.type = "GET";                
            options.data = { countryID: Id };
            options.dataType = "json";
            options.contentType = "application/json";
            debugger;
            options.success = function (slist) {
                $("#statename").empty();
                for (var i = 0; i < slist.length; i++) {                      
                    $("#statename").append('<option value="' + slist[i].Value + '">' + slist[i].Text + '</option>');
                }
                $("#statename").prop("disabled", false);
            };
            options.error = function () { alert("Error retrieving states!"); };
            $.ajax(options);
        }            
        else {
            $("#statename").empty();
            $("#statename").prop("disabled", true);
        }
    });

@Jaimin非常感謝您的幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM