0

I have this code:

  login: any;

  constructor(private http: HttpClient) { }

  ngOnInit() {
    this.http.get("http://localhost:8000/login/list")
      .subscribe(
        resultado => {
          this.login = resultado;
          document.write(this.login);
        }
      );
  }

I tried to get data from a JSON located on the web and I got the data! However, when I try to get it from the localhost server (which is running a PHP application and returning JSON) it doesn't seems to work. I tried to open the URL on the web browser and it does actually return a JSON.

Any clue on what's going on?

2
  • Your angular request may be not authenticated... do you authenticate in the browser before getting your JSON? Commented Jan 27, 2023 at 15:19
  • 1
    Or maybe there's a CORS error or something...it's impossible for us to diagnose or suggest anything based on the information provided so far. "doesn't work" isn't an error message or a useful problem statement - we learn nothing at all from such a vague statement. You need to do some debugging and narrow down the issue. Try opening your browser's Developer Tools, running the angular request and then seeing what happens in the Network tool and the Console. You should get some clues about the outcome of sending that request to /login/list. See also How to Ask for more guidance. Thanks. Commented Jan 27, 2023 at 15:33

1 Answer 1

1

Problem was CORS.

Adding this to my PHP project made it work:

composer req cors --ignore-platform-req=ext-http 
Sign up to request clarification or add additional context in comments.

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.