2015-09-01 4 views
-1

Я пытался вызвать ниже ajax для GetCustomerEntities. но я получаю result.length & responseJSON undefined.Please, дайте мне знать, если я что-то упустил? jquery ver. : 1.10.2 полный ответ 200, в порядке, но в случае ошибки, я получаю неожиданный маркер 5, parseerror:Ajax call return responseJSON.length undefined

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; 
    } 
+0

'complete' срабатывает независимо от того, что происходит. если ваш серверный скрипт ничего не отправляет, или отправляет обратно поврежденный json, вы ВСЕГДА будете «закончить» огонь. вам нужно также проверить «успех» и «ошибка». или, по крайней мере, посмотреть, что такое статус завершения: 'complete: function (obj, status) {console.log (status); } ' –

+0

В инструкции' debugger' добавьте: 'console.dir (result)' - результат уже должен быть массивом 'CustomerResult' и как таковой не будет' responseJSON'. jquery auto-convertts от json до вызова вашей полной функции. Также: ваш 'GetCustomerEntites' мог бы вернуть' JsonResult', но я не пробовал его так, как у вас есть, поэтому может быть и так. –

+0

@MarcB: он возвращается 200 – Dev

ответ

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); 
        }); 

       } 
      } 

     });