Ajax服务器响应json

  服务器响应json

服务器端口:

//响应一个json数据
const data={
    name:'stfgadf',
    agr:18
};
//对对象进行字符串转换
let str=JSON.stringify(data);
//设置响应体
response.send(str);

 

客户端端口:

//判断请求是否成功
if
(chr.status>=200&&xhr.status<300){ //1.手动对数据转换,进行json转换 let data=JSON.parse(xhr.response); //2.自动转换:设置响应体数据的类型,这样传进来的字符串会自动转换为已经设置好的类型 xhr.responseType='json'; console.log(xhr.response); result.innerHTML=xhr.response.name/agr;//进来的时候就会自动转换为json }

 

posted @ 2020-10-18 19:42  焕不涣  阅读(107)  评论(0编辑  收藏  举报