1

Given this simplified scenario:

export class LoginComponent{ 
   grant_type: string="password"; 
   jsonPayload: string; 

   Login(username, password){
      this.jsonPayload = JSON.stringify(username, password, this.grant_type);
   }
}

It looks like stringify is confused by TypeScript's "this". So, how do I make well-formed JSON, here?

Thanks,

0

1 Answer 1

3

stringify accepts three arguments, which are:

  • The thing to stringify
  • The replacer function to use
  • The indentation to use

You're passing it a non-function (password) as the second argument.

You probably mean to pass it one argument, an object to stringify:

this.jsonPayload = JSON.stringify({
    username,
    password, 
    grant_type: this.grant_type
});

or if you want to be explicit with all three since the last one needs it:

this.jsonPayload = JSON.stringify({
    username: username,
    password: password, 
    grant_type: this.grant_type
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, T.J.! I've got well-formed JSON, now. Only need to fix a niggling preflight bug...

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.