NativeScript + Angular failed to connect to localhost/127.0.0.1


#1

I have created a backend using Symfony and I have created this url:
http://localhost/Projects/SmartERP/web/app_dev.php/api/utility/send-pointage-mail
The objective of this url is to get data and return json result
The problem I’m facing now is that I can’t connect to this URL using NativeScript and Angular’s Http module
and I get this exception:

System.err: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
System.err: 	at libcore.io.IoBridge.connect(IoBridge.java:124)
System.err: 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
System.err: 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
System.err: 	at java.net.Socket.connect(Socket.java:882)
System.err: 	at com.android.okhttp.internal.Platform.connectSocket(Platform.java:139)
System.err: 	at com.android.okhttp.Connection.connect(Connection.java:148)
System.err: 	at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
System.err: 	at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
System.err: 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
System.err: 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
System.err: 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
System.err: 	at org.nativescript.widgets.Async$Http$RequestOptions.writeContent(Async.java:313)
System.err: 	at org.nativescript.widgets.Async$Http$HttpRequestTask.doInBackground(Async.java:536)
System.err: 	at org.nativescript.widgets.Async$Http$1.run(Async.java:482)
System.err: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
System.err: 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
System.err: 	at org.nativescript.widgets.Async$PriorityThreadFactory$1.run(Async.java:52)
System.err: 	at java.lang.Thread.run(Thread.java:818)
System.err: Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
System.err: 	at libcore.io.Posix.connect(Native Method)
System.err: 	at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
System.err: 	at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
System.err: 	at libcore.io.IoBridge.connect(IoBridge.java:122)
System.err: 	... 17 more
JS: ERROR Error: Uncaught (in promise): Response with status: 200  for URL: null

and this is my service:

import {Injectable} from "@angular/core";
import "rxjs/add/operator/map";
import {Http} from "@angular/http";

@Injectable()
export class MailerService{
    private API_URL = 'http://localhost/Projects/SmartERP/web/app_dev.php/api/utility/send-pointage-mail';

    constructor(private http: Http){}

    sendMail(){
        console.log('sending to ' + this.API_URL);
        return this.http.post(this.API_URL, JSON.stringify(object))
                        .map(res => res.json());
    }

}

The url is working fine when I enter it via a browser.
What am I missing here ?
Thanks in advance.


#2

OMG I’m such a noob I forgot that localhost is running on my pc not in the emulator
that’s why the device does not know this URL.
Sorry :slight_smile: