向REST API发送JSON – 访问控制允许来源 [错误]

huangapple go评论74阅读模式
英文:

Sent Json to REST API - Access Control Allow Origin [ERROR]

问题

以下是您提供的内容的翻译:

我想将 JSON 发送到我的 REST API。我已经在 Postman 中尝试过,并且运行良好。现在我想从我的前端发送数据。我不知道在哪里添加头部以及如何添加。我在后端使用 Spring Boot,在前端使用 Vue。

我的控制器:

@RestController
@RequestMapping("/api/auth")
public class FileController {

    FileService fileService;

    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @RequestMapping(
            value = "/data",
            method = RequestMethod.POST,
            consumes = "application/json")
    public void process(@RequestBody String payload) throws Exception{
        System.out.println(payload);

        try {
            FileWriter writer = new FileWriter(...);
            writer.write(payload);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我的 main.js 文件,用于发送数据:

const toSend = {
    "name": "test",
    "test1": "test2"
};

const jsonString = JSON.stringify(toSend);

const xhr = new XMLHttpRequest();

xhr.open("POST", "http://localhost:8090/api/auth/data");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(jsonString);
console.log(jsonString);

请注意,翻译内容可能会因格式和排版的不同而有细微的变化。

英文:

I want to send a json to my REST API. i tried it with Postman and it works fine. Now i want to sent Data from my frontend. I dont know where i need to add the header and how i do it. Im using Springboot for my BackEnd and Vue for my FrontEnd.
My Controller:

@RestController
@RequestMapping("/api/auth")
public class FileController {

FileService fileService;

public FileController(FileService fileService) {
    this.fileService = fileService;
}

@RequestMapping(
        value = "/data",
        method = RequestMethod.POST,
        consumes = "application/json")
public void process(@RequestBody String payload) throws Exception{
    System.out.println(payload);

    try {
        FileWriter writer = new FileWriter(...);
        writer.write(payload);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
   }
 }

My main.js, which shall send the data:

const toSend ={

"name": "test",
"test1":"test2"
 };

const jsonString = JSON.stringify(toSend);

const xhr = new XMLHttpRequest();

xhr.open("POST", "http://localhost:8090/api/auth/data");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(jsonString);
console.log(jsonString)

答案1

得分: 0

你需要为这个控制器启用跨域(CORS)功能。
有一个相应的注解可以实现这一功能。
请查阅以下指南:
https://spring.io/guides/gs/rest-service-cors/

英文:

You need to enable cors for this controller.
There is an annotaion for that matter.
Check out this guide
https://spring.io/guides/gs/rest-service-cors/

答案2

得分: 0

你确定通过xhr发送是最好的解决方案吗?为什么不使用fetch?
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

你的Vue应用和Spring Boot应用运行在不同的端口上,浏览器将其视为不同的网站,这就是为什么会因为跨源资源共享(CORS)而阻止。另一个解决方案是在js端添加CORS标头,也可以在代理上进行处理。

英文:

Are you sure that sending by xhr is best solution? why don't you use fetch?
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

Yours vue app and spring boot apps runs on different ports and browser sees it separate websites that why it blocks because of cors. Other solution that adding cors headers, can be proxy on js side.

huangapple
  • 本文由 发表于 2020年9月22日 04:42:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63999726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定