1

How do I fetch the data in the response-variable. In the debugger I can see all the data sent back (image) but how do I fetch the information from Object?

function getUserList() {
    $.ajax({
        url: "https://reqres.in/api/users",
        type: "POST",
        data: {
            name: "paul rudd",
            movies: ["I Love You Man", "Role Models"]
        },
        success: function(response) {

            console.log(response);

        }
    });

}

I am following the example at

https://reqres.in/

enter image description here

Talha Awan
  • 4,070
  • 4
  • 21
  • 39
java
  • 1,015
  • 18
  • 40
  • 1
    Use `console.log(response.variableName)`. Where `variableName` will be your variable what you sent in your `data` and let me know if it works. – Ankit Singh Jul 29 '17 at 12:40
  • @AnkitSingh - thanks it works good but when it comes to accessing movies which is an array it wont work. If I try with response.movies[0] for instance I get an "uncaught typeerror: cannot read property 0 of undefined) – java Jul 30 '17 at 12:22

1 Answers1

0

Create an global variable in required format and assign ajax post value to it. But make sure object properties first then create the object. Otherwise you need to extent it by using jquery extend() method

var userObject = {createdAt:"",id:"",movies:[],name:""};

function getUserList() {

$.ajax({
    url: "https://reqres.in/api/users",
    type: "POST",
    data: {
        name: "paul rudd",
        movies: ["I Love You Man", "Role Models"]
    },
    success: function(response){
        userObject = response;
        console.log(userObject.createdAt+" "+ userObject.id);

    }
});
Ananth Cool
  • 125
  • 8
  • thanks - but how do I fetch content of the movies-variable.? Tried with userObject.movies[0] for the first indexposition – java Jul 29 '17 at 12:59
  • Use the same logic. Create a movies object and access movies array with help of that object. – Ananth Cool Jul 30 '17 at 05:46