试图使用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