3
votes

I'm getting this error when trying to do a POST.

Response with status: 200 for URL: null ** {"_body":{},"status":200,"ok":true,"statusText":"","headers":{},"type":3,"url":null} **

This is my code

import { Injectable } from "@angular/core";
import { Http, Headers } from "@angular/http";
import "rxjs/Rx";

let LocalStorage = require( "nativescript-localstorage" );

import { User } from "../";
import { BackendService, Package, Pack, Order, Address, Reason } from "../";

@Injectable()
export class PackageService {
  public constructor(private http: Http) { }
  token : string = LocalStorage.getItem('token');
  userId : number = LocalStorage.getItem('userId');

  getPackages() {
    let headers = new Headers();
    let url = BackendService.apiUrl + BackendService.apiNameSpace + "mobile/packinglist/list";
    headers.append("Content-Type", "application/json");
    headers.append("Authorization", "Bearer " + this.token);
    this.http.post(url, {userId: this.userId}, { headers: headers})
      .map(result => JSON.parse(result.json()))
      .do(result => console.log("RESULT: ", JSON.stringify(result)))
      .subscribe(result => {
          //TODO
      }, error => {
          console.log("ERROR: ", error);
      });
  }
}

and my packages.config

{
  "description": "FM Transportes",
  "license": "SEE LICENSE IN <your-license-filename>",
  "readme": "FM Transportes",
  "repository": "<fill-your-repository-here>",
  "nativescript": {
    "id": "org.nativescript.FMTransportes",
    "tns-android": {
      "version": "2.5.0"
    }
  },
  "dependencies": {
    "@angular/common": "2.2.1",
    "@angular/compiler": "2.2.1",
    "@angular/core": "2.2.1",
    "@angular/forms": "2.2.1",
    "@angular/http": "2.2.1",
    "@angular/platform-browser": "2.2.1",
    "@angular/platform-browser-dynamic": "2.2.1",
    "@angular/router": "3.2.1",
    "email-validator": "1.0.4",
    "nativescript-angular": "1.2.0",
    "nativescript-localstorage": "^1.1.0",
    "nativescript-sqlite": "^1.1.2",
    "nativescript-telerik-ui": "^1.5.1",
    "nativescript-theme-core": "^0.2.1",
    "reflect-metadata": "~0.1.8",
    "rxjs": "5.0.0-beta.12",
    "tns-core-modules": "2.3.0"
  },
  "devDependencies": {
    "babel-traverse": "6.18.0",
    "babel-types": "6.18.0",
    "babylon": "6.13.1",
    "lazy": "1.0.11",
    "nativescript-dev-typescript": "^0.3.2",
    "typescript": "^2.0.8",
    "zone.js": "~0.6.21"
  }
}

****UPDATE**** I've tried to use the nativescript http component to do the request (https://docs.nativescript.org/cookbook/http) but occurred an other error while trying to parse to JSON:

Uncaught (in promise): SyntaxError: Unexpected token � in JSON at position 0

i console.log the response before JSON.stringfy and the result was:

enter image description here

I tryied the same request at postman and the response was correct.

EDIT I tried with javascript XMLHttpRequest

var request = new XMLHttpRequest();
    request.open('POST', "http://api.alfatracking.com.br/homolog/api/v1/mobile/packinglist/list", true);
    request.setRequestHeader('Authorization', "Bearer ...thetoken...");
    request.setRequestHeader('Content-Type', 'application/json');
    request.send(JSON.stringify({userId: "4f235762-d0f2-e611-901e-002197e04fb6"}));

    request.onreadystatechange = () => {
      console.log("finished");
      console.log(request.readyState);
      console.log(request.responseText);
      console.log(request.responseType);

and the response was the following: enter image description here

but on web browser the same code works. enter image description here

3
did you resolve this?Tinus Jackson
@TinusJackson the problems is simples: nativescript does not works with endpoints with gzip content.Douglas Franco

3 Answers

2
votes

I was stuck with the same error. All I did is to use the secure protocol https instead of http

As my server is on Heroku, http was not working.

0
votes

Try changing your map to this:

.map(result => result.json())

You may need to map the data a level down depending how it's returned:

.map(result => result.json().data)
0
votes

I had the same issue. I was trying to reach a webservice that was accessible via localhost. Seems like this is not working in nativescript.

Solution: Use http://0.0.0.0 instead of http://localhost.