简体   繁体   English

登录的callApi访问.json文件-vuejs

[英]callApi with Login to access .json File - vuejs

i want to access a .json file via link. 我想通过链接访问.json文件。 when i type in the link to my json file in my browser, it asks for credentials (username + password) which i know. 当我在浏览器中键入指向json文件的链接时,它会要求提供我知道的凭据(用户名+密码)。

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. 我想在代码中写入凭据,因此当网站尝试从json文件中获取数据时,我不必再手动登录,也不会收到一条消息以我的凭据登录。

of course if there are other possibilities to access the json other than a callApi method you're welcome. 当然,除了callApi方法之外,如果还有其他访问json的可能性,欢迎您。 :) :)

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/ 因此,示例网址为https:// username:password@www.example.com/

Note that this is not a good practice and should not be used in production. 请注意,这不是一个好习惯,不应在生产中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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