Failed to load resource: the server responded with a status of 403

超、凢脫俗 2022-04-11 11:20 490阅读 0赞
  1. Failed to load resource: the server responded with a status of 403 ()
  2. personList.html:1 Failed to load http://192.168.0.103/person/getList:
  3. Response to preflight request doesn't pass access control check:
  4. No 'Access-Control-Allow-Origin' header is present on the requested resource.
  5. Origin 'http://127.0.0.1:8020' is therefore not allowed access. The response had HTTP status code 403.

前后端联调时,出现如上的错误,可能是因为contentType指定不恰当或者后端要求数据格式不匹配引起的

  1. 后端不要求json格式,但是前端是json格式

    前端:

    1. $(function(){
    2. $.ajax({
    3. type:"get",
    4. url:"http://192.168.0.103/person/getList",
    5. dataType:"json",
    6. contentType:"application/json;charset=utf-8",
    7. success:function(result){
    8. alert(result);
    9. }
    10. });
    11. });

    后端:

    1. @RequestMapping(value = "/getList", produces = MediaType.APPLICATION_JSON_VALUE)
    2. @ResponseBody
    3. public ResultBean getList() {
    4. return new ResultBean(getData());
    5. }
  2. 后端要求是json,前端传的数据是form表单序列化$("#form").serialize()
    前端:

    1. $(function(){
    2. $.ajax({
    3. type:"post",
    4. url:"http://192.168.0.103/person/createByJson",
    5. dataType:"json",
    6. data:$("#form").serialize(),
    7. contentType:"application/json;charset=utf-8",
    8. success:function(result){
    9. alert(result);
    10. }
    11. });
    12. });

    后端:

    1. @PostMapping(value = "/createByJson", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    2. @ResponseBody
    3. public ResultBean createByJson(@RequestBody @Validated Person person) {
    4. return new ResultBean(person);
    5. }

发表评论

表情:
评论列表 (有 0 条评论,490人围观)

还没有评论,来说两句吧...

相关阅读