1
我想做一個Ajax調用,並從PHP腳本接收json,然後將在js代碼中處理。JQuery沒有接收json數據結構只是empyt []
因爲當JSON傳遞給回調函數的空數組某種原因...所以console.log(json)
給[]
然而,當我打電話PHP腳本它給了我有效的JSON作爲的["7494","7495","7565"]
輸出瀏覽器。
這是AJAX/JQuery的:
$.ajax({
url:'validation_ajax.php',
data:{
'jobids': 'yes',
'projectid': '3062'
},
type: 'GET',
dataType: 'json',
success: function(d){
console.log(d);
},
error: function(xhr, status, errorThrown) {
alert("Sorry, there was a problem!");
console.log("Error: " + errorThrown);
console.log("Status: " + status);
console.dir(xhr);
}
});
的PHP是這樣
$projectId = testInput($_GET['project']);
$con = dbConnect();
$sql = "select distinct iJobID from Validation_Samples
where iProjectID = $projectId";
$result = mysqli_query($con, $sql);
$jobs = array();
while($row = mysqli_fetch_array($result)){
$jobs[] = $row['iJobID'];
}
echo json_encode($jobs);
mysqli_close($con);
相關:http://stackoverflow.com/questions/5147522/jquery-ajax-request-of-datatype-json-will-not-retrieve-data-from-php-script – Und3rTow