構(gòu)建Wep App 2018(三)|前后端跨域資源共享|Cors+Axios+Vue+Express

前言

為解決Vue前端與Node.js服務(wù)端端數(shù)據(jù)請(qǐng)求傳輸問題,由于服務(wù)端和前端端口不同,數(shù)據(jù)請(qǐng)求跨域,故使用Axios與Cors實(shí)現(xiàn)Ajax跨域通信。

Vue前端:"http://localhost:8080"
Node.js服務(wù)端:"http://localhost:3000"


服務(wù)端配置

  • 安裝Cors組件
npm install cors --save
  • 服務(wù)器跨域設(shè)置
    main.js
var express = require("express");
var cors = require("cors");
var app = express();

app.use(
  cors({
    origin: ["http://localhost:8080"],
    methods: ["GET", "POST"],
    alloweHeaders: ["Content-Type", "Authorization"]
  })
);

app.get("/getNewsList", function(req, res, next) {
  res.send("get /getNewsList");
  console.log("get /getNewsList");
});

app.get("/vueTest", function(req, res, next) {
  res.send("get /vueTest");
  console.log("get /vueTest");
});
app.listen(3000);
console.log("[Listening on port 3000...");

也可添加以下命令,允許任意跨域訪問

app.all("*", function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", " 3.2.1");
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});


前端配置

  • 安裝axios組件
npm install axios --save
  • 安裝cors及vue-cors組件
npm install cors --save
npm install vue-cors --save
  • 配置前端入口腳本
    main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";
import axios from "axios";
import VueAxios from 'vue-axios'
import Vuetify from "vuetify";
import "vuetify/dist/vuetify.min.css";


Vue.use(VueAxios,axios);
Vue.use(Vuetify);

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  components: { App },
  template: "<App/>"
});
  • 測(cè)試
    在服務(wù)端app.js中添加
app.get("/getNewsList", function(req, res, next) {
  res.send("get /getNewsList");
  console.log("get /getNewsList");
});

app.get("/vueTest", function(req, res, next) {
  res.send("get /vueTest");
  console.log("get /vueTest");
});

在任一components中加入

<script>
export default {
  name:'vcardNews',
  data(){
      return{
          clists:[]
      }
  },
  methods:{
    /*
      https://www.npmjs.com/package/vue-axios
    */
    getNewsList(){
      this.axios.get('http://localhost:3000/getNewsList').then((response) => {
        alert(response.data);
        this.clists = response.data;
      })
    }
  },

  mounted(){
    this.getNewsList()
  }
}
</script>
  • 測(cè)試成功



?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 以下是蓋茨先生的十條玉言,經(jīng)常閱讀,我認(rèn)為大有好處。 1 . 社會(huì)充滿不公平現(xiàn)象。你先不要想去改造它,只能先適應(yīng)它...
    BUG弄潮兒閱讀 501評(píng)論 0 0
  • 老姐打來電話,開始就是稀里嘩啦的一通抱怨。說感覺現(xiàn)在整天都不知道干什么,一天也就幾節(jié)課,閑時(shí)間很多,卻愣是不知道干...
    jiao我樹人閱讀 454評(píng)論 0 0
  • 和你相識(shí),已經(jīng)快六年了。在這將近六年的時(shí)間里,我不得不承認(rèn),你帶給我更多的是快樂。我也忘記,當(dāng)初我和你是如何相識(shí)...
    不想睡覺的豆子閱讀 963評(píng)論 0 3

友情鏈接更多精彩內(nèi)容