試圖使用jQuery
和AJAX
在PHP
中加載JSON
文件中的內容,但該函數僅返回[object Object],[object Object],[object Object]
。在PHP中使用jQuery和AJAX加載JSON文件的問題
這裏是JSON文件。
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
這裏是我使用的代碼。
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function() {
$.ajax({
url: 'testing.txt',
type: 'GET',
dataType: 'json',
success: function(result) {
alert(result['employees']);
},
error: function() {
alert("error");
}
});
});
});
</script>
</head>
<body>
<div id="div1">
<h2>Let jQuery AJAX Change This Text</h2>
</div>
<button>Get External Content</button>
</body>
</html>
我在做什麼錯?
嘗試提醒結果,看看是否它的對象,它是正確的,你有3個對象 –
這是正確的,'result'是一個對象,而'result.employees'是一個數組三個對象...問題是你如何訪問數據...'result.employees [0] .firstName'將* John *例如 –
@JaromandaX啊,這很有道理。謝謝! – TheAuzzieJesus