2015-08-26 103 views
-2

我正在處理typeahead.js腳本,但沒有得到正確的響應,我嘗試了很多東西,但沒有正常工作。 這個網址是從PHP文件返回傑森數據...Uncaught TypeError:無法讀取undefined json數據的屬性「長度」

var result = (function() 
{ 
    var val = document.getElementById('sel_fac').value; 
    var city = document.getElementById('select_city').value; 
    var query = document.getElementById('typeaheadfield').value; 
    var url = 'search.php?fac=' + val + '&city=' + city + '&key=' + query; 
    console.log(url); 
    makeRequest(url, function(data) { 
     var data = JSON.parse(data.responseText); 
     console.log(data); 
     return data; 
    }); 
    function makeRequest(url, callback) { 
     var request; 
     if (window.XMLHttpRequest) { 
      request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari 
     } else { 
      request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5 
     } 
     request.onreadystatechange = function() { 
      if (request.readyState == 4 && request.status == 200) { 
       callback(request); 
      } 
     } 
     request.open("GET", url, true); 
     request.send(); 
    } 
})(); 
// console.log(result); 

var substringMatcher = function(strs) { 
console.log(strs); 
    return function findMatches(q, cb) { 
     var matches, substringRegex; 
     // console.log(strs); 
     window.alert(strs); 
     // an array that will be populated with substring matches 
     matches = []; 

     // regex used to determine if a string contains the substring `q` 
     substrRegex = new RegExp(q, 'i'); 

     // iterate through the pool of strings and for any string that 
     // contains the substring `q`, add it to the `matches` array 
     $.each(strs, function(i, str) { 
      if (substrRegex.test(str)) { 
       matches.push(str); 
       window.alert(str); 
       // console.log(str);  
      } 
     }); 

     cb(matches); 
    }; 
}; 

,當我用這個傑森數據得到正確的結果努力。

// var result1 = [「金牌健身房Hiranandani」,「Golds Gym Marol Naka」,「金牌健身房Chembur」,「金牌健身房Bandra West」,「金牌健身房Kandivali East」,「金牌健身房下部Parel」, 「Gold's Gym Vashi」,「金牌健身房Kandiwali west」,「金牌健身房Mulund West」,「金牌健身房 - 莫娜健身中心私人有限公司」,「西部黃金健身房Borivali」,「Jignya Johri @金牌健身房」,「金牌健身房Thane西區「,」金牌健身房西哈爾「,」金牌健身格蘭道(W)「];

$(document).ready(function() { 

    $('#typeaheadfield').typeahead({ 
     hint: true, 
     highlight: true, 
     minLength: 1, 
     limit: 10 
    }, 
    { 
     name: 'fac_name', 
     source: substringMatcher(result) 
    }); 
    window.alert(result); 
    console.log(result ? result.length : 'result is null or undefined'); 
}); 

回答

0

您的函數設置結果是異步的。它不能立即返回數據,所以當它等待它時,它會繼續執行下面的代碼。爲防止出現這種情況,請使用回撥:

$(document).ready(function() { 

    var result = []; // <--------------------------Define the variable (empty array) 

    (function() { 
    /* ... */ 
    makeRequest(url, function(data) { 
     var data = JSON.parse(data.responseText); 
     //return data; <--------------------------Remove this 
     result = data; // <------------------------Set the value of result 
     keepGoing(); // <--------------------------Execute a callback 
    }); 
    /* ... */ 
    })(); 

    console.log(result); // <----------------------Empty array, data not received yet 

    function keepGoing() { 
    console.log(result); // <--------------------Should be available now 
    } 

}); 
相關問題