所以我試圖從PHP發送數據到js。
我搞砸了JSON對象,數組和字符串
PHP
$balkTypes[] = $stmt->fetchAll();
echo json_encode($balkTypes);
JS
balkTypesData = {}; //Outside Ajaxcall
success: function(result){
balkTypesData = result;
Console.log(balkTypesData);
}
控制檯現在
[[{"id":"3","typ":"Bas 200*600","hojd":"200","bredd":"600","rec":"","viktM":"135"},{"id":"2","typ":"Bas 240*600","hojd":"240","bredd":"600","rec":"","viktM":"160"},{"id":"5","typ":"Isol\u00e4tt 240*600","hojd":"240","bredd":"600","rec":"","viktM":"105"},{"id":"4","typ":"Kontur 240*600","hojd":"240","bredd":"600","rec":"","viktM":"105"},{"id":"6","typ":"Passbit","hojd":"0","bredd":"0","rec":"","viktM":"0"}]]
,我倒是喜歡大海我的Json對象?!
我倒是想找個 「viktM」 爲 「典型值:巴斯200 * 600」 的所有
//Get balkType weight/m
var searchField = "typ";
var searchVal = "Bas 200*600";
for (var i=0 ; i < balkTypesData.length ; i++){
if (balkTypesData[i][searchField] == searchVal) {
weigth = balkTypesData[i]['viktM'];
console.log(weigth);
}
}
首先,它接縫,我不能在 「balkTypsData」 使用.lengt
。它給了我410次點擊。必須是所有角色?
其次,我找不到如何訪問我的對象的一部分。
如果我使用:console.log(balkTypesData[i][searchField]);
我得到:「未定義」
我也試圖以刪除「[我]
所以我缺少什麼
溫柔i'm還在學習。?。
你是在正確的軌道上......看看'JSON.parse()來' - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON /解析 – corn3lius