0

My title say it all, I invoke an API which in return sends the data in JSON format, which I want to assign to a variable. here is my ajax code. Remember: I'm using it in an Angular application

I've tried to assign it to a variable, but it didn't worked.

$.ajax({
  url: 'https://randomuser.me/api/',
  dataType: 'json',
  success: function(data) {
    console.log(data);
  }
});

1 Answer 1

1

You will have to use arrow function in order to get correct context as:

$.ajax({
  url: 'https://randomuser.me/api/',
  dataType: 'json',
  success: (data) => {
    console.log(data);
    this.abcVariable = data; //<====== Here

  }
});

Note: It's not good practice to use jQuery in Angular application, Angular provides http module for network things.

Sign up to request clarification or add additional context in comments.

2 Comments

alright, but this code doesn't work as it is. it shows error on $ sign Member 'summary' implicitly has an 'any' type.
You can check stackoverflow.com/questions/30623825/… for jQuery with Angular

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.