从这个通过ajax请求加载的链接https://maps.googleapis.com/maps/api/directions/json?origin=Jalan+Kaliurang&destination=Jalan+Malioboro中,如何只提取和显示html指令。
在这里,html代码
<!DOCTYPE html>
<html>
<head>
<title>Coba Ajax JQuery</title>
<script type="text/javascript" src="jquery-1.12.3.js"></script>
<script type="text/javascript" src="jquery.xdomainajax.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#driver').click(function(event) {
$.ajax({
cache: false,
url: "https://maps.googleapis.com/maps/api/directions/json?origin=Jalan+Kaliurang&destination=Jalan+Malioboro",
type: "GET",
success: function(data) {
var string_data = JSON.stringify(data);
$('#stage').html(string_data);
},
error: function() {
alert("error");
}
});
});
});
</script>
</head>
<body>
<input type="button" id="driver" value="Load Data" />
<div id="stage">STAGE</div>
</body>
</html>发布于 2016-05-08 05:03:49
您需要使用JSON.parse而不是JSON.stringify。从AJAX调用获得的响应已经是一个JSON。你需要解析它。
Data_obj=JSON.parse(数据);
这个data_obj会有点复杂,因为它将创建元素数组。因此,为了访问html_instructions,需要运行具有正确数组索引的循环。如果你能告诉我你想要使用的确切的html_instructions,我可以给你访问它的代码。
如果你想自己弄清楚的话。只需console.log(data_obj)就可以看到结构,并相应地编写循环。
https://stackoverflow.com/questions/37091043
复制相似问题