0

I'm getting api json data for weather widget. It's return null. I am getting json data very well. But i can't handle this value.

Here my html;

<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script> 
</head>
<body>
  <script>
    /* $(document).ready(function(){
      $.getJSON(url, function(json){
        console.log(json.location.name);
      })
    })/*/
  </script>
  <div id="app" >{{Country}}</div>
  <p v-if="isLoading">Loading</p>
  <script type="text/javascript" src="./js/app.js"></script>
</body>     
</html>

here my js;

window.addEventListener('load',()=>{
  var app = new Vue ({
    el: '#app',
    data:{
        Country : null,
        Temp : null,
        Icon : null,
        isLoading : true,
    },
    created(){
      var url ="http://api.apixu.com/v1/current.json?key=a69259fba9904700964121622190801&q=Nazilli";
        $.getJSON(url,function(json){
            console.log(json);
            this.isLoading = false;
            this.Country = json.location.name;
            console.log(this.Country);
        });
      },
   })
});
brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
Mert
  • 474
  • 2
  • 8
  • 21

2 Answers2

1

You're losing this context in $.getJSON callback: it doesn't point to Vue instance inside function call.

You should ether bind this to function directly:

$.getJSON(url, (function (json) {
  //...
}).bind(this));

Or use fat arrow function:

$.getJSON(url, (json) => {
  //...
});
Styx
  • 9,863
  • 8
  • 43
  • 53
-1

The scope of this change inside getJSON function. You must save the parent this

var url ="http://api.apixu.com/v1/current.json?key=a69259fba9904700964121622190801&q=Nazilli";
var me = this; // save this in me variable
        $.getJSON(url,function(json){
            console.log(json);
            me.isLoading = false;
            me.Country = json.location.name; // use me instead this
            console.log(me.Country);
        });