0

I have a project that uses jQuery, and I have a lot of functions that return json, and I am trying to populate a data variable with some test data but when I loop through the data I am getting nothing.

I have checked to make sure that my ajax call is returning the json data and it is, and I am kind of at a loss on why nothing is being returned

<div id="app">
   <template v-for="person in persons">
       <div>
           {{ person.FirstName }}
       </div>
   </template>
</div>

<script>
    new Vue({
        el: '#app',
        created: function () {
            this.GetUserDetails();
        },
        data: {
            persons: []
        },
        methods: {
            GetUserDetails() {
                this.persons = CommonFunctions.GetJSON("Person");
            }
        }
    });
</script>

Here is some of the Person json,

[{
    "id": 1,
    "FirstName": "Auroora",
    "LastName": "Kosel",
    "gender": "Female"
  },
  {
    "id": 2,
    "FirstName": "Nevins",
    "LastName": "Rulf",
    "gender": "Male"
  },
  {
    "id": 3,
    "FirstName": "Silvana",
    "LastName": "Cragoe",
    "gender": "Female"
 }]

** EDIT ** Here is the GetJSON function

var CommonFunctions = {
    GetJSON: (whichOne) => {
        $.ajax({
            type: "GET",
            url: "../Scripts/" + whichOne + "JSON.json",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                return data;
            }
        });
    }
}
4
  • If you put console.log(this.persons) in GetUserDetails, do you get proper value? Commented Oct 28, 2018 at 12:14
  • comes back as undefined Commented Oct 28, 2018 at 12:15
  • 1
    Can you include code of CommonFunctions.GetJSON function? Commented Oct 28, 2018 at 12:16
  • Just made an edit and added it Commented Oct 28, 2018 at 12:19

2 Answers 2

2

Your GetJSON() function doesn't return anything.

var CommonFunctions = {
    GetJSON(whichOne)  {
      return $.getJSON("../Scripts/" + whichOne + "JSON.json");
//    ^^^^^^
    }
}

Now it returns the request/promise (and uses jQuery's shorthand method for retrieving json).

In your component you'd write

async GetUserDetails() {
  this.persons = await CommonFunctions.GetJSON("Person");
}

Or if you don't like async/await:

 CommonFunctions.GetJSON("Person").then(data => {
   this.persons = data;
 });
Sign up to request clarification or add additional context in comments.

6 Comments

If I add a console.log(data) into the success of the GetJSON it returns the data
Weird, place a console.log(this.persons) below the this.persons = CommonFunctions.GetJSON("Person"); line, what does it log?
returns undefined
Exactly, because the GetJSON doesn't return anything/undefined. I'll update my answer with working GetJSON method...
I've updated my answer with a GetJSON implementation that returns the request/promise.
|
1

As my understanding, ajax function is asynchronous, so it doesn't return directly

There is a simple workaround:

   methods: {
        GetUserDetails() {
            this.persons = CommonFunctions.GetJSON("Person", this);
        }
    }

and in helper function:

var CommonFunctions = {
    GetJSON: (whichOne, self) => {
        $.ajax({
            type: "GET",
            url: "../Scripts/" + whichOne + "JSON.json",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                self.persons = data
                return data;
            }
        });
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.