在我的开发环境中,我有一个使用Spring构建的BE,还有一个使用React.js (Redux + Axios)构建的FE。每当我试图使用Axios执行HTTP POST时,就会被禁止使用403。HTTP调用如预期一样工作,从Postman发出的HTTP调用也正常工作,也很好。
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
发布于 2017-04-24 13:59:17
发现问题了!这其实是CORS的问题。我必须从请求开始的领域对某人启用CORS。答案在here上发布。
https://stackoverflow.com/questions/43597984
复制相似问题