其他分享
首页 > 其他分享> > Access to XMLHttpRequest at ‘localhost:8080/drug/queryDrug‘ from origin ‘http://localhost:8082‘

Access to XMLHttpRequest at ‘localhost:8080/drug/queryDrug‘ from origin ‘http://localhost:8082‘

作者:互联网

报错信息:这个是跨域问题8082访问8080阻止了

在这里插入图片描述
解决方案:在Controller类上架@CrossOrigin

package com.fh.controller;


import com.h.entity.Drug;
import com.h.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/drug")
@CrossOrigin
public class DrugController {
    @Autowired
    private DrugService drugService;

    //查询数据
    @RequestMapping("queryDrug")
    public List<Drug>queryDrug(){
        return drugService.queryDrug();
    }
    //新增
    @RequestMapping("addDrug")
    public void addDrug(Drug drug){
         drugService.addDrug(drug);
    }

}


如果还不行查看自己的请求url路径,必须加http://协议

 url:'http://localhost:8080/drug/queryDrug',

错误版:

<template>
  <div id="app">

  </div>
</template>

<script>
export default {

  name: 'App',
  created(){
    this.queryDrug();
  },
  methods:{
     queryDrug () {
    this.axios({
      method:'get',
      url:'localhost:8080/drug/queryDrug',
      data:{
      }
    }).then(function (resert) {

      console.log(resert.data);
    }).catch(function (ss) {
      console.log(ss);
    })
  }
  },



}
</script>


正确版:

<template>
  <div id="app">

  </div>
</template>

<script>
export default {

  name: 'App',
  created(){
    this.queryDrug();
  },
  methods:{
     queryDrug () {
    this.axios({
      method:'get',
      url:'http://localhost:8080/drug/queryDrug',
      data:{
      }
    }).then(function (resert) {

      console.log(resert.data);
    }).catch(function (ss) {
      console.log(ss);
    })
  }
  },



}
</script>


展示结果:
在这里插入图片描述

标签:origin,8080,ss,drug,queryDrug,import,localhost
来源: https://blog.csdn.net/jq1223/article/details/114281532