PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] How to write data from an AJAX request to DATA in VUE JS?

 September 08, 2022     ajax, javascript, vue.js, vuejs2     No comments   

Issue

Tell me please, how in DATA to write data from the AJAX response? Example:

var example = new Vue({
    el: '#example',
    data:{
        myArr: []
    },
    created: function () {
        $.getJSON('data.json', function(data) {
            this.myArr = data;
        });
    }
  });

The problem is that in myArr, the response data is not written. How to solve this? Thank you.


Solution

Can you try this ? Basically this inside the ajax is not exactly the one you would expect.

var example = new Vue({
    el: '#example',
    data:{
        myArr: []
    },
    created: function () {
        var vm = this;
        $.getJSON('data.json', function(data) {
            vm.myArr = data;
        });
    }
  });

You can also use reactivity setting method $set instead of directly assigning to the vm.myArr : https://v2.vuejs.org/v2/guide/reactivity.html



Answered By - Mihir Bhende
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing