Vscode前端与springboot链接

问题描述 投票:0回答:2

我用 vscode(html、sass 和 js 节点模块)构建了前端,用 springboot 构建了后端。如何将 springboot 后端链接到 vscode 前端?

java node.js spring-boot visual-studio-code node-modules
2个回答
0
投票

使用 Spring Boot 开发所有必要的 API 以获取后端数据。然后,要在前端获取数据,您可以使用 ExpressJs(Node.js 的 Web 应用程序框架)。


0
投票

要将 SpringBoot 后端服务器与 SpringBoot 运行应用程序连接起来,假设在 localhost 8080,那么您只需知道您的后端 Api 是否正常工作,因为 Postman 是最好的工具。然后正如你所说,你有数据,但你只需从服务器发布或获取它,然后你可以在特定的 URL 处使用来自前端的 XML Http 请求,并且肯定会获取有效负载/将其发布到服务器,这里是 XML Http 请求示例:-

// Define the data you want to send to the server
const dataToSend = {
  name: "John Doe",
  email: "[email protected]",
};

// Create a new XMLHttpRequest object
const xhr = new XMLHttpRequest();

 
xhr.open("POST", "http://localhost:8080/api/create", true);


xhr.setRequestHeader("Content-Type", "application/json");
 
xhr.onload = function () {
  if (xhr.status === 200) {
    // Request was successful, handle the response data here
    const responseData = JSON.parse(xhr.responseText);
    console.log("Response from server:", responseData);
  } else {
    // Request encountered an error, handle the error here
    console.error("Error:", xhr.status, xhr.statusText);
  }
};

 
xhr.onerror = function () {
  console.error("Network error occurred");
};

 
const jsonData = JSON.stringify(dataToSend);

 
xhr.send(jsonData);

不要忘记将数据字符串化以转换为 JSON 格式。

这只是前端与SpringBoot应用程序连接的一种工作方式。

学习愉快。:)

© www.soinside.com 2019 - 2024. All rights reserved.