javascript - vue 2.3 AJAX data binding not updating -
after successful ajax call data rendered on page not updating. remaining empty / null.
it seems missing how connect data returned front end variable , dynamically / live rendered html elements.
here relevant code snippets context. clear missing / incorrect?
javascript
page = new vue({ el: "#container", data: { option_id: null, option_name: null }, created:function() { $.ajax({ type: 'post', contenttype: 'application/json', datatype: 'json', url: 'ajax_methods/get_option', success: function (ajax_data) { self = this; self.option_id = ajax_data.option_id; self.option_name = ajax_data.option_name; }, error: function (e) { console.log(e) } }) } })
html
<script type="text/javascript" src="https://unpkg.com/vue@2.3.3"></script> <div id="container"> <p>{{ option_name }}</p> <button v-on:click="send_option()" type="button" id="left_button" name="left_button" v-bind:value="option_id"> </div>
checking ajax success
when entering following in console, non null values come expected:
- self.option_id
- self.option_name
first, if exact code, self
don't think initialized. use var self = this
or let self = this
but mainly, need define self
outside of ajax call. in javascript this
keyword refers calling object. directly inside of created()
function, it's vue instance. however, this
not refer vue instance once inside ajax callback.
understand javascript’s “this” clarity, , master it
created:function() { var self = $.ajax({ type: 'post', contenttype: 'application/json', datatype: 'json', url: 'ajax_methods/get_option', success: function (ajax_data) { self.option_id = ajax_data.option_id; self.option_name = ajax_data.option_name; }, error: function (e) { console.log(e) } }) }
Comments
Post a Comment