猿问

在C#MVC Framework中通过Ajax调用在对象上进行迭代迭代

我在通过AJAX脚本中的C#MVC框架中的控制器返回的对象进行迭代时遇到问题。


Ajax脚本:


function getRName(obj) {

    $.ajax({

        url: 'resourceNamePopup',

        method: 'post',

        data: { "search": obj.value },

        dataType: 'json',

        success: function (data) {

            alert('success');

            $.each(data.items, function (items) {

                alert(items.displayName);

            });


        }

    });

}

C#动作:


    public ActionResult resourceNamePopup(string search)

    {

        List<ResourceName> rnList = new List<ResourceName>();

        rnList = getResourceName(search);


        return Json(rnList );

    }

我现在得到这个错误


http:// localhost:52273 / Scripts / jquery-1.10.2.js中第645行第4列的未处理异常


0x800a138f-JavaScript运行时错误:无法获取未定义或空引用的属性“ length”


杨魅力
浏览 162回答 2
2回答

30秒到达战场

我想指出两个问题您不需要添加到&nbsp;List<object>$.each(data.items, function (items)&nbsp;部分不正确您需要返回rnList(从方法resourceNamePopup()&nbsp;[还请使方法和属性名称以大写字母开头])。在AJAX响应中使用以下命令:function getRName(obj) {&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: 'resourceNamePopup',&nbsp; &nbsp; &nbsp; &nbsp; method: 'post',&nbsp; &nbsp; &nbsp; &nbsp; data: { "search": obj.value },&nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('success');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(data, function (i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(data[i].displayName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}
随时随地看视频慕课网APP
我要回答