簡體   English   中英

如何從服務器發送一組 json 文件?

[英]How do I send an array of json files from the server?

我正在使用 vue.js。 我已經編寫了以下代碼,但它不起作用,我該如何解決?

這是我的代碼:

 mounted(){
    fetch('/', {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *cors, same-origin    
    headers: {
       'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
   
  })
    .then(response => response.json())    
    .then(json => this.firstWindow = json)
    
    // .then(json => console.log(json))
    // .then(console.log(this.firstWindow))
    
  },

服務器部分在這里

router.post('/',(req, res) =>{  // request for sending file
    console.log("POST");
    // res.sendFile(myModule.dataOfScreens);
    res.sendFile(path.resolve('./data/firstScreen.json'));
})

您正在嘗試從服務器獲取文件,但您使用的是POST方法!!!

如果這是你想要做的,那么你應該做這樣的事情:

router.get('/filesFolder?:fileName', function(req, res){
    let filePath = '.. the path of your local files folder ..+ '/' +req.query.fileName
    if (fs.existsSync(filePath)) {
      //file exists
      res.sendFile(filePath);
    }
  });

其中 filesFolder 是您存儲文件的文件夾的名稱; fileName 是您請求的文件的名稱; filePath 是所請求文件的 filesFolder 下的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM