薰 风 2021-06-26 13:07 采纳率: 63.3%
浏览 264
已采纳

前端vue中一直报跨域,以下如何解决

1接口地址 http://film.glkjjt.com/api/Movie/New  这个地址直接放在浏览器中能运行

2在main.js中的代码

import Vue from "vue";

import App from "./App";

import router from "./router";

import axios from "axios";

Vue.prototype.$http = axios;

axios.defaults.baseURL = "http://film.glkjjt.com/api";

Vue.config.productionTip = false;

new Vue({

  el: "#app",

  router,

  components: { App },

  template: "<App/>"

});

3.在config中index.js中的代码

"use strict";

const path = require("path");

module.exports = {

  dev: {

    assetsSubDirectory: "static",

    assetsPublicPath: "/",

  // 在proxyTable中配置的跨域

    proxyTable: {

      "/api": {

        target: "http://film.glkjjt.com",

        changeOrigin: true,

        pathRewrite: {

          "^/api": ""

        }

      }

    },

    host: "localhost", 

    port: 8080, 

    autoOpenBrowser: false,

    errorOverlay: true,

    notifyOnErrors: true,

    poll: false,

    devtool: "cheap-module-eval-source-map",

    cssSourceMap: true

  },

  build: {

    index: path.resolve(__dirname, "../dist/index.html"),

    assetsRoot: path.resolve(__dirname, "../dist"),

    assetsSubDirectory: "static",

    assetsPublicPath: "/",

    productionSourceMap: true,

    devtool: "#source-map",

    productionGzip: false,

    productionGzipExtensions: ["js", "css"],

    bundleAnalyzerReport: process.env.npm_config_report

  }

};

4在components文件夹中HelloWorld.vue中的代码

template>

  <div class="hello">

    <h1>{{ msg }}</h1>

  </div>

</template>

<script>

export default {

  name: "HelloWorld",

  data() {

    return {

      msg: "Welcome to Your Vue.js App"

    };

  },

// 在生命周期里面发请求

  created() {

    this.$http

      .get("api/Movie/New")

      .then(response => {

        console.log(1);

        console.log(response);

      })

      .catch(error => {

        console.log(2);

        console.log(error);

      });

  }

};

</script>

// 接口文档中发这个get请求不需要参数

5 通过npm run dev运行

在浏览器中出现的报错信息

  • 写回答

2条回答 默认 最新

  • 永远不会太晚 2021-06-26 14:05
    关注

    把axios.defaults.baseURL = "http://film.glkjjt.com/api";删了,配置proxy代理不能设置axios.defaults.baseURL 

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法