<!DOCTYPE html><html> ?<head> ???<meta charset="utf-8"> ???<title></title> ???<script> ?????window.onload=function(){ ???????let aBtn=document.getElementById(‘btn‘); ???????aBtn.onclick=function(){ ?????????let xhr=new XMLHttpRequest(); ?????????xhr.open(‘get‘,‘data/2.txt‘,true); ?????????xhr.send(); ?????????xhr.onreadystatechange=function(){ ???????????if(xhr.readyState==4){ ?????????????if(xhr.status==200){ ???????????????alert(‘成功‘); ???????????????let json=null; ???????????????try { ?????????????????json=JSON.parse(xhr.responseText); ???????????????} catch (e) { ?????????????????json=eval(‘(‘+xhr.responseText+‘)‘); ???????????????} finally { ?????????????????console.log(11); ???????????????} ???????????????console.log(json); ?????????????} ???????????} ?????????} ???????} ?????} ???</script> ?</head> ?<body><input type="button" name="" value="提交" id="btn"> ?</body></html>
1.接收响应数据:
???xhr.responseText ???文本数据
???xhr.responseXML ????xml数据
?2.eval——不安全
?3.JSON
???JSON.stringify ???????{a: 12, b: 5} ??=> ???"{\"a\": 12, \"b\": 5}"
???JSON.parse ???????????"{\"a\": 12, \"b\": 5}" => {a: 12, b: 5}
???JSON——不兼容
?4.安全:
???1.前台没有安全性;后台才有问题(sql注入)
???2.xss——跨站脚本攻击
???ajax不允许跨域
?5.json标准格式:
???1.key必须用引号包起来
???2.双引号
???{"a": 12, "name": ‘blue‘}
AJAX
原文地址:http://www.cnblogs.com/chaofei/p/7820165.html