3đź‘Ť
âś…
first, if that is the exact code, then self
I don’t think is initialized. Use var self = this
or let self = this
But mainly, you need to define self
outside of the ajax call. In javascript the this
keyword refers to the calling object. directly inside of the created()
function, it’s the Vue instance. However, this
will NOT refer to the Vue instance once inside the ajax callback.
Understand JavaScript’s “this” With Clarity, and Master It
created:function() {
var self = this
$.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)
}
})
}
👤ptpaterson
4đź‘Ť
You need to capture this
outside the callback.
created: function(){
const self = this;
$.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)
}
})
}
👤Bert
Source:stackexchange.com