Posting to PHP, return Response with status: 200 for URL: null
Angular2
Android emulator.
Trying to post simple values to a PHP script, Nativescript always return "JS: Response with status: 200 for URL: null".
I tried different tutorials and docs examples, same result.
Tried using http.post, same result.
PHP script works well tested with other options.
All dependencies are up-to-date.
app.module.ts:
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import {NativeScriptFormsModule} from "nativescript-angular/forms"
import { AppRoutingModule } from "./app.routing";
import { AppComponent } from "./app.component";
import { ItemService } from "./item/item.service";
import { ItemsComponent } from "./item/items.component";
import { ItemDetailComponent } from "./item/item-detail.component";
import { NativeScriptHttpModule } from "nativescript-angular";
@NgModule({
bootstrap: [
AppComponent
],
imports: [
NativeScriptModule,
AppRoutingModule,
NativeScriptFormsModule,
NativeScriptHttpModule
],
declarations: [
AppComponent,
ItemsComponent,
ItemDetailComponent
],
providers: [
ItemService
],
schemas: [
NO_ERRORS_SCHEMA
]
})
/*
Pass your application module to the bootstrapModule function located in main.ts to start your app
*/
export class AppModule { }
main.ts:
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NgModule } from "@angular/core";
import { AppComponent } from "./app.component";
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { appComponents, appRoutes } from "./app.routing";
import { NativeScriptHttpModule } from "nativescript-angular";
@NgModule({
declarations: [AppComponent, ...appComponents],
bootstrap: [AppComponent],
imports: [
NativeScriptModule,
NativeScriptRouterModule,
NativeScriptRouterModule.forRoot(appRoutes),
NativeScriptHttpModule
],
})
class AppComponentModule {}
platformNativeScriptDynamic().bootstrapModule(AppComponentModule);
my-service.ts:
```
import { Http, Headers, Response, RequestOptions, URLSearchParams } from "@angular/http";
import { Observable } from "rxjs/Observable";
import "rxjs/add/operator/do";
import "rxjs/add/operator/map";
import "rxjs/add/observable/throw";
import "rxjs/add/operator/catch";
@Injectable()
export class MyService {
constructor(private http: Http) {}
postData() {
var url = "http://localhost/my-project/api/php/my-script.php";
var headers = new Headers();
headers.append("Content-Type", "application/json");
var params:URLSearchParams = new URLSearchParams();
params.append("name", "Mike");
var options: RequestOptions = new RequestOptions();
options.headers = headers;
options.url = url;
options.method = "POST";
options.params = params;
this.http.request(url, options).subscribe(
(success) => {
console.log(success);
},
(error) => {
console.log(error);
}
);
}
}
implementation in a component.ts:
this.myService.postData();
```
RESULT:
System.err: java.net.ConnectException: Connection refused
System.err: at java.net.PlainSocketImpl.socketConnect(Native Method)
System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
System.err: at java.net.Socket.connect(Socket.java:605)
System.err: at com.android.okhttp.internal.Platform.connectSocket(Platform.java:113)
System.err: at com.android.okhttp.Connection.connectSocket(Connection.java:196)
System.err: at com.android.okhttp.Connection.connect(Connection.java:172)
System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
System.err: at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:257)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:1133)
System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
System.err: at org.nativescript.widgets.Async$PriorityThreadFactory$1.run(Async.java:52)
System.err: at java.lang.Thread.run(Thread.java:761)
JS: Response with status: 200 for URL: null
Hello @ParVisual the issue is that you are trying to access your local server via the loopback address localhost but when working on emulators this loopback is not valid and you should use the one that is set up for the chosen emulator. See this thread for details
Instead, localhost use 10.0.2.2 (if using AVD emulator)
10.0.2.2 - Special alias to your host loopback interface
(i.e., 127.0.0.1 on your development machine)
For GenyMotion emulator the loopback address is 10.0.3.2
Hi, 10.0.2.2 returns "404 not found", I opted to upload my php scripts to a remote server and now works fine.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Hello @ParVisual the issue is that you are trying to access your local server via the loopback address
localhostbut when working on emulators this loopback is not valid and you should use the one that is set up for the chosen emulator. See this thread for details