鍍金池/ 問答/HTML5  HTML/ vuex+koa 前端取不到數(shù)據(jù)

vuex+koa 前端取不到數(shù)據(jù)

http://localhost:3000/getRencentJob  這個是一個json數(shù)據(jù)的接口
// 注意require('koa-router')返回的是函數(shù):
const router = require('koa-router')()
const fs=require('fs')
const cors=require('koa-cors')

router.get('/getRencentJob',async (ctx, next)=>{
    await cors();
    ctx.body=JSON.parse(fs.readFileSync('./static/recentJob.json'))
    
})


module.exports=router

我前端8080 想跨域取數(shù)據(jù)

const actions = {
    getJson(context){
    // 調(diào)用我們的后端getJson接口
    fetch('http://127.0.0.1:3000/getRencentJob', {
      method: 'GET',
      mode:'cors',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
    }).then(function (res) {
        console.log(res)
      if(res.status === 200){
          
          
        return res.json()
      }
    }).then(function (json) {

     // console.log(typeof Array.from(json), Array.from(json));
      context.commit('setJson', json);
    })
  }
};

還是報錯

recentJob.js?a162:18 OPTIONS http://localhost:3000/getRencentJob 404 (Not Found)
getJson @ recentJob.js?a162:18
wrappedActionHandler @ vuex.esm.js?358c:704
dispatch @ vuex.esm.js?358c:426
boundDispatch @ vuex.esm.js?358c:332
getJson @ dashboard.vue?07b0:81
mounted @ dashboard.vue?07b0:87
callHook @ vue.esm.js?efeb:2921
insert @ vue.esm.js?efeb:4158
invokeInsertHook @ vue.esm.js?efeb:5960
patch @ vue.esm.js?efeb:6179
Vue._update @ vue.esm.js?efeb:2660
updateComponent @ vue.esm.js?efeb:2788
get @ vue.esm.js?efeb:3142
Watcher @ vue.esm.js?efeb:3131
mountComponent @ vue.esm.js?efeb:2795
Vue.$mount @ vue.esm.js?efeb:8540
Vue.$mount @ vue.esm.js?efeb:10939
Vue._init @ vue.esm.js?efeb:4640
Vue @ vue.esm.js?efeb:4729
(anonymous) @ main.js?1c90:16
./src/main.js @ app.js:3856
webpack_require @ app.js:679
fn @ app.js:89
0 @ app.js:3897
webpack_require @ app.js:679
(anonymous) @ app.js:725
(anonymous) @ app.js:728
:8080/#/:1 Failed to load http://localhost:3000/getRencentJob: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
vue.esm.js?efeb:8555 Download the Vue Devtools extension for a better development experience:
https://github.com/vuejs/vue-...
:8080/#/:1 Uncaught (in promise) TypeError: Failed to fetch

該怎么寫呀 新手上路

回答
編輯回答
背叛者

const cors=require('koa2-cors')

app.use(cors({

origin: function (ctx) {
   
        return "*"; // 允許來自所有域名請求
   
},
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
maxAge: 5,
credentials: true,
allowMethods: ['GET', 'POST', 'DELETE'],
allowHeaders: ['Content-Type', 'Authorization', 'Accept'],

}))

2018年1月30日 22:24
編輯回答
假灑脫

vue config 中設(shè)置 proxy 也可以跨域,不過僅限開發(fā)的時候使用,打包后無效。

2017年11月15日 17:55
編輯回答
嘟尛嘴

另一個答案正解

需要為KOA配置一個koa-cors來允許跨域請求。

因為在跨域的時候,瀏覽器會去后端查詢headers是否列出了支持跨域的方法和域。
所以需要在后端提供這個headers,最簡單的就是koa-cors

2017年10月12日 15:02
編輯回答
陪她鬧

跨域了 , 安裝koa-cors,注意版本是koa1還是koa2的

2017年5月31日 08:10