Ajax调用返回responseJSON.length undefined

时间:2015-09-01 19:02:56

标签: jquery ajax asp.net-mvc

我试图在ajax下面调用GetCustomerEntities。但我得到了result.length& responseJSON未定义。如果我错过了什么,请告诉我? jquery ver。 :1.10.2 完成响应是200,好的但是在出错时,我得到了意外的令牌5,parseerror:

 var loadmyEntities = function (customerId, sucessCallBK) {
                $.ajax({
                    type: 'GET',
                    url: 'http:/localhost:15343/Customer/GetCustomerEntities',
                    async: true,
                    dataType: 'json',
                    data: { customerId: customerId},
success: function (result) {
                debugger
                if (result !== null && result.length !== 0) {
                    debugger
                            renderCustomerEntities(result, function () {
                                sucessCallBK(result);
                            });

                        }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(textStatus, errorThrown);
            },
                    complete: function (result) {
                        debugger
                        if (result !== null && result.length !== 0) {
                            renderCustomerEntities(result, function () {
                                sucessCallBK(result);
                            });

                        }
                    }

                });

        };


var renderCustomerEntities = function (custEntity, callBk) {
    if (!(typeof custEntity.responseJSON == "string")) {
        var count = 0;
        if (custEntity.responseJSON === undefined) {
            count = custEntity.length;
        } else {
            count = custEntity.responseJSON.length;
        }
   }
}


public IEnumerable<CustomerResult> GetCustomerEntities(string customerId)
    {
        List<CustomerResult> parts = new List<CustomerResult>();

        // Add parts to the list.
        parts.Add(new CustomerResult() { CustomerName = "crank arm", CustomerAddress = 1234 });
        parts.Add(new CustomerResult() { CustomerName = "chain ring", CustomerAddress = 1334 });
        parts.Add(new CustomerResult() { CustomerName = "regular seat", CustomerAddress = 1434 });
        parts.Add(new CustomerResult() { CustomerName = "banana seat", CustomerAddress = 1444 });
        parts.Add(new CustomerResult() { CustomerName = "cassette", CustomerAddress = 1534 });
        parts.Add(new CustomerResult() { CustomerName = "shift lever", CustomerAddress = 1634 });

        IEnumerable<CustomerResult> obj = parts;
        return obj;
    }

1 个答案:

答案 0 :(得分:0)

不熟悉responseJson但是如果你想返回JSON,你应该创建一个JSON结果

public JsonResult GetCustomerEntities(string customerId)
{           
    return Json(__customerRepsitory.GetCustomerEntities(customerId), JsonRequestBehavior.AllowGet);
}

是啊..并使用成功而不是完整的

       $.ajax({
            type: 'GET',
            url: 'http:/localhost:15343/Customer/GetCustomerEntities',
            async: true,
            data: { customerId: customerId},
            success: function (result) {
                debugger
                if (result !== null && result.length !== 0) {
                    renderCustomerEntities(result, function () {
                        sucessCallBK(result);
                    });

                }
            }

        });