簡體   English   中英

API 和使用 axios 的接口之間的通信

[英]Communication between API and Interface using axios

I have I have a problem to etablish a communication between the API and the Interface To be more clear: I send a JSON file from the API, but I can't recover it and display it in the Interface, The JSON is verry simple: {“名稱”:“喬”}

這是我的代碼:在 API 中:( http://localhost:8080/route1

const jsontest=require('../models/test'); 
.
.
router.post('/', (req,res)=>{
       res.send(jsontest);  
        });
module.exports=router;

並在接口端( http://localhost:8081/

<template>
.
.
  <v-btn x-large color="grey"  class="d-flex align-start flex-column mb-6" @click="getData" 
 >Start</v-btn>


      <div v-if="todos">
        <li>{{todos}}</li>
      </div>
</template>

其次是:

<script>

import axios from 'axios';
const WorkersURL='http://localhost:8080/route1';

export default {
 data(){
   return{
     drawer: false,
      todos:''
   }
 },  

 methods: {
 getData:function()
 {
    axios.post(WorkersURL)
    .then(response => {
      console.log(response.data) ;
      this.todos= response.data.name;    
          })
    .catch(e => {
      this.errors = e
    });

  }
},
 mounted(){
     this.getData
  }
}
</script>

任何幫助,將不勝感激

你必須在你的服務器中使用res.json()

router.post('/', (req,res)=>{
  res.send(jsontest);  
});

並刪除axios.post(WorkersURL,{}) {}

暫無
暫無

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

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