Detailed description of how to use AJAX to obtain data in vue, vueajax

Source: Internet
Author: User

Detailed description of how to use AJAX to obtain data in vue, vueajax

During VUE development, jquery and vue-resource can be used to obtain data. When obtaining data, you must give an initial data value.

See the following example:

<script type="text/javascript">     new Vue({      el:'#app',      data:{data:""},      created:function(){        var url="json.jsp";        var _self=this;        $.get(url,function(data){          _self.data=eval("(" + data +")");        })        /*        this.$http.get(url).then(function(data){          var json=data.body;          this.data=eval("(" + json +")");        },function(response){          console.info(response);        })*/      }     });  </script>

The initial data of vue data must be set here, even if the data is empty at this time.

Vue-resource is more suitable when ajax is used to obtain data.

The vue-resource code is as follows:

<script type="text/javascript">     new Vue({      el:'#app',      data:{data:""},      created:function(){        var url="json.jsp";              this.$http.get(url).then(function(data){          var json=data.body;          this.data=eval("(" + json +")");        },function(response){          console.info(response);        })      }     });  </script>

Here we can see that when setting VUE instance data, you can directly use this. data to set vue data.

When using jquery, the Code is as follows:

<script type="text/javascript">   new Vue({      el:'#app',      data:{data:""},      beforeCreate:function(){        var url="json.jsp";        var _self=this;        $.get(url,function(data){          _self.data=eval("(" + data +")");        })      }     });  </script>

Here, you need to assign this value to _ self for later use in jquery's get method, so that it is not convenient to use vue-resource.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.