-1
通過使用Stringify,我可以檢索數據,但我不知道如何將它傳遞給JavaScript變量。它通過[{「0」:「1」,「ItemNumberFK」:「1」},{「0」:「2」,「ItemNumberFK」:「2」},{「0」:「3」 ItemNumberFK 「:」 3" }]我不知道,但我想取數1,2,3只有我怎麼能做到這一點如何將數組從Ajax請求傳遞給Javascript變量
JQUERY:
$(document).ready(function() {
$("#OrderPackageNumber").change(function(event){
// You just get the value of selected input
// You don't need to find anything because you've already selected it
var selectedd = $(this).val();
alert(selectedd);
//id_numbers = new Array();
var displayString = new Array();
$.ajax({
url:'getitemsofpackage.php',
dataType:'json',
type:'post',
data:{ namee: selectedd },
success: function(data) {
console.log(data);
alert(JSON.stringify(data));
},error: function(stats, exception){
var msg = '';
if (stats.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (stats.status == 404) {
msg = 'Requested page not found. [404]';
} else if (stats.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + stats.responseText;
}
alert(msg);
// return false;
}
});
});
});
</script>
PHP:
$result = $conn->query("SELECT ItemNumberFK FROM tblpackageitem WHERE PackageNumberFK = '".$namee."' ;");
while($row = mysqli_fetch_array($result)){
//echo '<option value ="' . $row['PackageNumber'] . '" '. $selected .'>' . $row[1] . '</option>';
//echo '<input type="checkbox" value= "' . $row['ItemNumber'] . '" >' . $row['ItemName'];
//echo $row['ItemPrice'];
//echo "<br>";
$arrayOfItems[] = $row;
// $strTry = $row;
}
// play with return result array
//while($row = mysql_fetch_array($result1)){
//$selectBoxOption1 .="<option value = '".$row['bike_type']."'>".$row['bike_type']."</option>";
//}
// return options
echo json_encode($arrayOfItems);
你試過'JSON.parse'還是直接選擇它? (數據:{namee:selectedd}),' –
解析JSON數據。 obj => displayString.push(obj.ItemNumberFK))' – Curiousdev
數字將被推送到displayString變量:''JSON.parse(data).forEach('data [0] [0]'等) –