Nativescript: http.request return "Response with status: 200 for URL: null"

Created on 31 Oct 2017  路  3Comments  路  Source: NativeScript/NativeScript

Posting to PHP, return Response with status: 200 for URL: null

Which platform(s) does your issue occur on?

Angular2
Android emulator.

Please provide the following version numbers that your issue occurs with:

  • CLI: 3.3.0
  • Cross-platform modules:
    _nodeVersion": "6.11.5
    _npmVersion": "4.6.1
    "typings": "tns-core-modules.d.ts",
    "version": "3.3.0"
  • Runtime(s): 3.0.0
  • Plugin(s):
    "dependencies": {
    "@angular/animations": "~4.2.0",
    "@angular/common": "~4.2.0",
    "@angular/compiler": "~4.2.0",
    "@angular/core": "~4.2.0",
    "@angular/forms": "~4.2.0",
    "@angular/http": "~4.2.0",
    "@angular/platform-browser": "~4.2.0",
    "@angular/router": "~4.2.0",
    "nativescript-angular": "~4.2.0",
    "nativescript-google-maps-sdk": "^2.4.1",
    "nativescript-theme-core": "~1.0.2",
    "reflect-metadata": "~0.1.8",
    "rxjs": "~5.4.2",
    "tns-core-modules": "^3.3.0",
    "zone.js": "~0.8.2"
    },
    "devDependencies": {
    "babel-traverse": "6.4.5",
    "babel-types": "6.4.5",
    "babylon": "6.4.5",
    "lazy": "1.0.11",
    "nativescript-dev-typescript": "~0.5.0",
    "typescript": "~2.4.2"
    }

Please tell us how to recreate the issue in as much detail as possible.

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.

Is there code involved?

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

android

Most helpful comment

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

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings