将GET结果保存到文本文件中会导致无限循环

时间:2018-06-21 00:36:24

标签: javascript vue.js axios

我一直试图将从GET请求获得的字符串保存到文本文件中。由于某种原因,它最终陷入无限循环,如果我不使用unsigned __int128,那么它根本无法工作,created()只会保持空白。

story

使用HTML5功能保存文件是否明智?

2 个答案:

答案 0 :(得分:0)

Axios是基于Promise的HTTP API,因此删除async / await即可

getStory(id) {
      return new Promise((res, rej) => {
          var req = 'http://localhost:8080/api/story/' + id
          axios.get(req).then((response) => {
              res(response.data.story);
           }).catch((err) => {
              rej(err);
           });
      })
    },
    getLetter() {
      var story = this.getStory(this.$route.params.id);
      this.story = letter;
    }

另一件事,有循环,所以没有无限循环发生。而是,请求没有完成。

答案 1 :(得分:0)

我认为您可以在下面尝试以下代码

var app = new Vue({
  el: '#app',
 data () {
    return {
      story:"",
      url:"",
     
    }
  },
  created(){
	this.getLetter();
  },
  methods: {

        getStory:function(id) {
             var req = 'http://localhost/website-one-pages/slipt.php?id=' + id;
				 axios.get(req).then((response) => {
		              this.story = response.data;
		              
		              //set download
		              var data = [];
		              data.push(this.story);
		              var properties = {
			            type: 'text/plain'
			          };
			          try {
			            this.file = new File(data, "file.txt", properties);
			          } catch (e) {
			            this.file = new Blob(data, properties);
			          }
			          this.url = URL.createObjectURL(this.file);
			          //end set download

		           }).catch((error)=>{
                console.log(error);
              });
        },
        getLetter() {
          var id =2;//example id=2 
          this.getStory(id);
        },
    }
    
})
<div id="app">
    <p>{{story}}</p>
   
    <a id="link" v-bind:href="url" target="_blank" download="file.txt">Download</a>

相关问题