我试图创建一个具有以下功能的JS应用程序:一个获取JSON文档并显示该文本的按钮。我经历的Elasticsearch教程,也的确是在我公司提供的网址有效的JSON:使用JavaScript获取JSON响应并显示在网页上,测试JavaScript
{"_index":"planet","_type":"hacker","_id":"xfSJlRT7RtWwdQDPwkIMWg","_version":1,"exists":true, "_source" : {"handle":"mark","hobbies":["rollerblading","hacking","coding"]}}
使用下面的代码......我得到的
[object Object]
警报,而不是带有完整JSON的警报。我打算采取下一步实际选择部分JSON,但我想至少先看到完整的文档...
任何想法?先谢谢你!
<!DOCTYPE html>
<html lang="en">
<head><title>Demo</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /></head>
<body>
<input id="testbutton" type="button" value="Test" />
<p id="results">results appended here: </p>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#testbutton").click(function() {
$.ajax({
url: 'http://localhost:9200/planet/hacker/xfSJlRT7RtWwdQDPwkIMWg',
dataType: 'json',
success: function(data) {
$("#results").append('all good');
alert(data);
},
error: function() {
$("#results").append("error");
alert('error');
}
});
});
});
</script>
</body>
</html>
不是使用'alert',而是使用'console.log'。这将使您在未来的调试工作中更加轻松。 –
http://stackoverflow.com/q/29694300/4768363类似问题请通过。 –