• 标准化后端向前端传来的Json数据


    后端响应固定格式给前端:

    1,创建一个专门存储数据的类;分别存储响应状态码code,响应数据,传输的消息。

    1. public class CommonData {
    2. int code;
    3. Object data;
    4. String message;
    5. public CommonData(int code, String message) {
    6. this.code = code;
    7. this.message = message;
    8. }
    9. public CommonData(int code, Object data, String message) {
    10. this.code = code;
    11. this.data = data;
    12. this.message = message;
    13. }
    14. public int getCode() {
    15. return code;
    16. }
    17. public void setCode(int code) {
    18. this.code = code;
    19. }
    20. public Object getData() {
    21. return data;
    22. }
    23. public void setData(Object data) {
    24. this.data = data;
    25. }
    26. public String getMessage() {
    27. return message;
    28. }
    29. public void setMessage(String message) {
    30. this.message = message;
    31. }
    32. }

    2,后端向前端传输

    1. @Override
    2. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    3. String account=req.getParameter("account");
    4. String password=req.getParameter("password");
    5. System.out.println(account);
    6. System.out.println(password);
    7. PrintWriter printWriter=resp.getWriter();
    8. User user= null;
    9. CommonData commonData=null;
    10. try {
    11. user = dao.logindao(account,password);
    12. if(user==null) {
    13. commonData=new CommonData(201,"账号或密码输入有误,请重新输入!");
    14. }
    15. else {
    16. commonData=new CommonData(200,user,"登录成功");
    17. }
    18. } catch (SQLException throwables) {
    19. throwables.printStackTrace();
    20. commonData=new CommonData(500,"服务器忙...请稍后重试!");
    21. }
    22. ObjectMapper objectMapper=new ObjectMapper();
    23. String json=objectMapper.writeValueAsString(commonData);
    24. printWriter.print(json);
    25. }

    3,前端接收后端传来的Json数据

    1. login() {
    2. this.$http.post("LoginServlet", jsonToString(this.from)).then(resp => {
    3. if (resp.data.code == 201) {
    4. this.$message({
    5. message: resp.data.message,
    6. type: 'warning'
    7. });
    8. } else if (resp.data.code == 200) {
    9. this.$message({
    10. message: resp.data.message,
    11. type: 'success'
    12. });
    13. sessionStorage.setItem("account", resp.data.data.account);
    14. this.$router.push("/main"); //在js中进行路由导航
    15. }
    16. })
    17. }

  • 相关阅读:
    C语言中,“>>=”;“<<=”;“&=”;“|=”分别代表什么
    矩阵相乘详解
    Abbkine活细胞溶酶体染色试剂盒:高特异性,荧光稳定性强
    CSS案例-1.字体样式练习
    包分配并不是个好制度
    Starknet的去中心化路线图
    docker容器健康状态健康脚本
    Android问题笔记 - 关于SuperNotCalledException报错异常信息的解决方案
    分账管理有哪些功能?
    获取dubbo源码编译并导入idea以及启动入门项目dubbo-demo
  • 原文地址:https://blog.csdn.net/m0_71385141/article/details/133801480