2014-03-28 74 views
0

的未定義的屬性「長度」我試圖用example遺漏的類型錯誤:無法讀取上自動完成

爲自動完成,使我使用的代碼

<head> 
    <title>JSP Page</title> 
    <link rel="stylesheet" type="text/css" href="css/jquery-ui.css" /> 
    <script src="js/jquery-1.9.1.js"></script> 
    <script src="js/jquery-ui.js"></script> 

    <script> 

     $(function() { 
      function log(message) { 
       $("<div>").text(message).prependTo("#log"); 
       $("#log").scrollTop(0); 
      } 

      $("#productName").autocomplete({ 
       source: function(request, response) { 
        alert('hiiiii'); 
        $.ajax({ 
         url: "ajaxProduct", 
         dataType: "json", 
         data: { 
          qry: $("#productName").val(), 
          maxRows: 5 
         }, 
         success: function(data) { 
          alert('success'); 
          alert(data.productList); 

          response($.map(data.productList, function(item) { 
           alert(data.productList); 
           console.log(item); 
           return { 
            label: item.productName, 
            value: item.productName, 
            id: item.productId 
           } 
          })); 
         }, 
         error: function(data) { 
          alert(data.productList); 
          console.log(typeof data); 
          console.log(data); 
          alert('error'); 
         } 
        }); 
       }, 
       minLength: 1, 
       select: function(event, ui) { 
        log(ui.item ? 
        "Selected: " + ui.item.label : 
        "Nothing selected, input was " + this.value); 
        $("#productId").val(ui.item.id) 
       }, 
       open: function() { 
        $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); 
       }, 
       close: function() { 
        $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); 
       } 
      }); 
     }); 

    </script> 
</head> 

<body> 
Product Id <span id="productId"></span><br> 
Product Name <input type="text" name="productName" id="productName"/> 

</body> 

是表示alert('hii');alert('success');,然後在警示框中顯示undefined
在CONSOLE.LOG它顯示

event.returnValue is deprecated. Please use the standard event.preventDefault() instead. 
Uncaught TypeError: Cannot read property 'length' of undefined jquery-1.9.1.js:766 
Denying load of chrome-extension://flhdcaebggmmpnnaljiajhihdfconkbj/jquery-2.0.2.min.map. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension. (index):1 

的Jquery:766

arg is for internal usage only 
    map: function(elems, callback, arg) { 
     var value, 
      i = 0, 
      length = elems.length,//766 line number 
      isArray = isArraylike(elems), 
      ret = []; 

如何解決這個問題。

+0

你的結果數據格式是什麼?我的意思是,當你得到這個網址'ajaxProduct'時,你會得到什麼? –

+0

console.log(數據)成功看到你正在得到什麼??????? –

+0

它包含productid和產品名稱的arraylist – xrcwrn

回答

2

因爲你沒有得到在dataproductList試試success function一樣,

success: function(data) { 
    if(data.productList){ // check the existance of productList 
     alert(data.productList); 
     response($.map(data.productList, function(item) { 
      console.log(item); 
      return { 
       label: item.productName, 
       value: item.productName, 
       id: item.productId 
      } 
     })); 
    } 
} 

response data應該是這樣的,

{"productList":{"productId": 0, "productName": null} } 
+0

它顯示undefined – xrcwrn

+0

alert('success')顯示後的console.log(data)'Object {productId:0,productName:null}' – xrcwrn

+0

它應該像'{「productList」:{「productId」:0 ,「productName」:null}}'看到我更新的答案 –

0

2.IN你ajaxProduct.php,你是返回一個JSON編碼陣列。請確保它包含密鑰productList

$arr = array('productList' =>'somevalue' ...); 

echo json_encode($arr); 
0

您在後端沒有字段名稱,例如productList。使用以下;

response($.map(data, function(item) { 
    return { 
     label: item.productName, 
     value: item.productName, 
     id: item.productId 
    } 
})); 
+0

我也試過這個。沒有獲取數據 – xrcwrn

+0

殼牌我在這裏更新我的java代碼。你能否看到這個 – xrcwrn

+0

你能否在返回的部分顯示你的後端代碼?你在後端使用java嗎? –

相關問題