JS文件中的AJAX没有路由到函数

时间:2018-04-01 11:40:00

标签: javascript jquery ajax asp.net-mvc-4 razor

我有这个JS功能

function initAreaNames(data) {   

    var selectedItem = data.value;
    //var selectedItem = $(this).val();
    var ddlStates = $("#AreaName");
    var statesProgress = $("#areas-loading-progress");
    statesProgress.show();
   // alert("selectedItem " + selectedItem);
    $.ajax({
       cache: false,
       type: "GET",       
        url: '@(Url.Action("GetAllAreasByCategory" ,"Address"))',
        data: { "CategoryName": selectedItem },
        success: function (data) {              
            ddlStates.html('');
            $.each(data, function (id, option) {               
                ddlStates.append($('<option></option>').val(option.id).html(option.name));
            });
            statesProgress.hide();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('Failed to retrieve states. ' + thrownError + " " + ajaxOptions + " " + xhr + u);
            statesProgress.hide();
        }
    });
}

由于某种原因,我的GetAllAreasByCategory没有被调用。

我尝试过使用alert success,但是当我在AJAX calli之前使用alert确实得到了我预期的提醒时

我在.cshtml上测试了这段代码(将该函数重写为<script> ..</script>)并且它正常工作

这是我的.cshtml文件

@model ErrorSelector.Models.AddressModel

@{
    ViewBag.Title = "Index";
}
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="JSFunctions.js"></script>

<div>

    @Html.LabelFor(model => model.CategoryName, new {style = "font-size:72px" } )
    @Html.DropDownListFor(model => model.CategoryName, Model.AvailableCategories
                   , new { @onchange = "initAreaNames(this)", style = @"font-size:72px" })



</div>
<br />

0 个答案:

没有答案