Spring Boot + React.js Axios:403 Forbidden for HTTP POST calls

时间:2017-04-24 21:25:44

标签: spring-mvc reactjs spring-boot axios

在我的开发环境中,我有一个使用Spring Boot构建的BE和使用React.js(Redux + Axios)构建的FE。每当我尝试使用Axios执行HTTP POST时,我都会禁止403。 HTTP GET调用按预期工作,Postman的HTTP POST调用也正常工作

PersonController.java

@RestController
@RequestMapping(value = "/api/v1/person")
public class PersonController {

    @Autowired
    private PersonRepository mPersonDAO;

    @PostMapping
    public ResponseEntity<Person> create(@RequestBody Person person) {
        Person result = mPersonDAO.save(person);
        return new ResponseEntity<>(result, HttpStatus.CREATED);
    }
}

React.js服务

import axios from 'axios';

const BASE_URL = 'https://localhost:8080/api/v1';
const PERSON_API = BASE_URL + '/person'

export function syncUser (person, onSuccess) {
  axios({
    method: 'post',
    url: PERSON_API,
    data: {
      person
    }
  }).then((response) => {
      onSuccess(response);
    }).catch(function (error) {
      console.log(error);
    });
}

堆栈跟踪:

localhost/:1 XMLHttpRequest cannot load http://localhost:8080/api/v1/person. 
dispatchXhrRequest @ xhr.js:177
xhrAdapter @ xhr.js:12
dispatchRequest @ dispatchRequest.js:52

Response for preflight has invalid HTTP status code 403

1 个答案:

答案 0 :(得分:0)

发现问题了!这实际上是一个CORS问题。我必须从请求来自的域中启用SB上的CORS。答案已发布here