<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
// 1.创建ajax对象
var xhr = new XMLHttpRequest();
// 2.告诉Ajax对象要向哪发送请求,以什么方式发送请求
// 1)请求方式 2)请求地址 【即路由表中对应的路径】
xhr.open('get', 'http://localhost:3000/responseData');
// 3.发送请求
xhr.send();
// 4.获取服务器端响应到客户端的数据
xhr.onload = function (){
// console.log(typeof xhr.responseText)
var responseText = JSON.parse(xhr.responseText); // 将JSON字符串转换为JSON对象
console.log(responseText) // 测试:在控制台输出处理结果
var str = '<h2>'+ responseText.name +'</h2>'; // 将数据和html字符串进行拼接
document.body.innerHTML = str; // 将拼接的结果追加到页面中
}
</script>
</body>
</html>
305 Ajax服务器端响应的数据格式
猜你喜欢
转载自www.cnblogs.com/jianjie/p/12341541.html
今日推荐
周排行