服务器与客户端数据交换(Rest API)

服务器端

Rest API 输出普通对象

AjaxResponse jsonObj = AjaxResponse.success(body);
					log.info("{} json:{}",RestResponseBodyAdvice.class,jsonObj.toString());
					return jsonObj;
AjaxResponse(success=true, code=200, message=This is normal, 
content=UserVO(id=1, userName=michael, userPasswd=123456, 
userFirstName=1, userMiddleName=1, userLastName=1, 
userSex=2, userAddress=222, 
CreateTime=Mon Jul 10 14:12:57 CST 2023, 
userToken=null, userPortrait=))

客户端

设置 ‘application/json;charset=UTF-8;’ 后,浏览器才能正常解析Json 普通对象,否则不能识别。

客户端请求和响应格式

const axiosHelper = axios.create({
    
    
  baseURL: import.meta.env.VITE_API_URL,
  // withCredentials: true,
  // timeout: TIMEOUT,
  headers: {
    
    
    'Content-Type': 'application/json;charset=UTF-8;'
  }
})

转成成Json对象

const josnStr = JSON.parse(
        JSON.stringify(tokenInfo.value) || window.localStorage.getItem('Token') || '{
    
    }'
      )

转化成Json 字符串

JSON.stringify(tokenInfo.value)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/MyFreeIT/article/details/131748019