1

I have created a Rest API in Java with the request Mapping(/value/generate).

System.out.println(value);
        
        
        if(!value.equals(null)){
         return new ResponseEntity(value, HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.FORBIDDEN);
            
    }

Value is getting printed in Console when iam call this through postman.

But i need to print this value in my Angular console.

Below is code i have created in angular.

service.ts

generatevalue(): Observable<String> {
return this.http.get('https://localhost:8080/value/generate')

Component.ts

ngOnInit() {
     this.service.generatevalue().subscribe((Response) => {
      console.log(Response)
     
  }
 }

Please let me know where i went wrong.

5
  • What do you see in your console? Commented Feb 25, 2022 at 11:09
  • What does Observable<any> instead of Observable<String> give you? Commented Feb 25, 2022 at 11:11
  • You'll want to use http for localhost. Angular should still be smart enough to run it successfully nonetheless, but just a tip. Other than that, this could be related to CORS (an issue Postman would not run into). But as others have mentioned, check your console first and see what the actual API response is. Commented Feb 25, 2022 at 11:28
  • @Enrico It won't change anything since changing TypeScript types won't change the runtime javascript code. Commented Feb 25, 2022 at 11:39
  • @enrico Iam getting the desried value.Basicaly the value is nothing but token value.So iam getting that. Commented Feb 25, 2022 at 11:42

1 Answer 1

1

Your url should probably be "http://localhost:8080/value/generate". Did you check your console for errors? It should print an error 404 somewhere.

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

5 Comments

Thats Typo error @yochyo.I have edit now
It should probably be http instead of https. For https, you would need a certificate. Can you update your question with the error message you get?
iam not getting any error i just want to print the response in angular console
Try replacing your js code with this this.service.generatevalue().subscribe((Response) => { console.log(Response)}, error =>console.error(error)) it should print an error now
You saved my day Yochyo..!! That error log helped me to find my mistake.Now its working..thanks a lot man..~~

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.