0

I've tried everything there is to try. I've tried proxy, I've tried allowing CORS on my API. I think I've reached the extent of my Google searches. Is there really no way that I can test my app with a localhost API? I tried this, in my ionic.config.json I added this:

  "proxies": [
    {
      "path": "/api",
      "proxyUrl": "http://localhost:8080"
    }

Then in my post request, I did this:

this.http.post('http://localhost:8100/api/users', payload).map(res => res.json())
.subscribe(res => {
  console.log("The result is");
  console.log(res);
}, error =>{
  console.log(error);
});

The url of the API is http://localhost:8080/users

I've tried this in a lot of ways and I have been unsuccessful. I am using ionic serve to run my app. Please, anyone that can help me out with this. I am really desperate at this point.

plasmy
  • 99
  • 3
  • 9
  • 32
  • can you hit the API by other means? For example, through Postman? – DavidX Feb 01 '18 at 02:50
  • Yes. Precisely by Postman it works – plasmy Feb 01 '18 at 02:50
  • If you add an err => {console.log(err);} as a second parameter to the observable what do you get? – DavidX Feb 01 '18 at 02:53
  • Edited question to show error parameter. It just keeps on giving status 500. In the console I am getting this `Error: connect ECONNREFUSED 127.0.0.1:8080 at Object._errnoException (util.js:1003:13) at _exceptionWithHostPort (util.js:1024:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1194:14)` – plasmy Feb 01 '18 at 02:58
  • Curiosly, when I try localhost:8080 on my postman, it works. But when I try 127.0.0.1:8080 it doesn't. I thought they were supposed to be the same? – plasmy Feb 01 '18 at 03:00
  • Did you try the answers [here](https://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device)? I think that using the router asigned IP instead of 127.0.0.1 should work. – Carl Kroeger Ihl Feb 01 '18 at 03:02
  • In your question you're using port 8100 on the post call. Should that be 8080? – DavidX Feb 01 '18 at 03:06
  • I'm not using a device right now. I'm using the `ionic serve` command to run the app in my browser. – plasmy Feb 01 '18 at 03:06
  • Also you have /api/users, is the endpoint /users? – DavidX Feb 01 '18 at 03:07
  • @DavidX well I'm using 8100 because of the proxy. I thought that was how it worked. – plasmy Feb 01 '18 at 03:07
  • Oh sorry, forgot you we're using a proxy. – DavidX Feb 01 '18 at 03:09
  • As @CarlKroegerIhl mentioned, I'd suggest swapping in your router assigned Ip. – DavidX Feb 01 '18 at 03:11

0 Answers0