简体   繁体   中英

callApi with Login to access .json File - vuejs

i want to access a .json file via link. when i type in the link to my json file in my browser, it asks for credentials (username + password) which i know.

i want to write the credentials in the code, so i dont have to log in manually anymore, OR get a message to log in with my credentials when the website is trying to fetch the data from the json file.

of course if there are other possibilities to access the json other than a callApi method you're welcome. :)

present code without authentication and with a local file:

<script>
import jsonData from '../../static/json/test.json'
export default {
  name: 'dash',
  data() {
    return {
      data: ''
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.callApi()
        .then((responseData) => {
          this.data = responseData;

        })
    },
    callApi() {
      return Promise.resolve(jsonData)
    }
  }
}
</script>

In case you are using basic authentication you can add your username and password in the beginning of the url separated by a colon. So an example url would be https://username:password@www.example.com/

Note that this is not a good practice and should not be used in production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM