【问题标题】:Angular 2 + NativeScript Response with status: 200 for URL: nullAngular 2 + NativeScript 响应状态:200 对于 URL:null
【发布时间】:2017-07-06 17:32:29
【问题描述】:

我在尝试进行 POST 时收到此错误。

响应状态为:200,网址为:null ** {"_body":{},"status":200,"ok":true,"statusText":"","headers":{},"type":3,"url":null} **

这是我的代码

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);
      });
  }
}

还有我的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"
  }
}

****更新**** 我尝试使用 nativescript http 组件来执行请求(https://docs.nativescript.org/cookbook/http),但在尝试解析为 JSON 时发生了另一个错误:

未捕获(承诺中):SyntaxError: Unexpected token � in JSON at position 0

我 console.log 在 JSON.stringfy 之前的响应,结果是:

我在邮递员处尝试了相同的请求,并且响应正确。

编辑 我尝试使用 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);

响应如下:

但在网络浏览器上相同的代码可以工作。

【问题讨论】:

  • 你解决了吗?
  • @TinusJackson 问题很简单:nativescript 不适用于带有 gzip 内容的端点。

标签: angular rxjs nativescript


【解决方案1】:

试着把你的地图改成这样:

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

您可能需要根据数据的返回方式将数据向下映射:

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

【讨论】:

  • 我已经尝试过不使用地图和执行功能,但订阅捕获时出错。
  • 请求不传入map函数,直接catch。
  • 我已经为几个电话做了这个。写入 .map(result =&gt; result.json()).map(result =&gt; result.json().data) 将取决于返回数据的样子
  • 我明白了,问题是在地图函数被调用之前。即使我删除它也会发生错误。
  • 现在尝试摆脱 .do.subscribe 并注销 result.json() 是什么
【解决方案2】:

我有同样的问题。我试图访问可通过 localhost 访问的 Web 服务。似乎这在 nativescript 中不起作用。

解决方案:使用http://0.0.0.0 而不是http://localhost

【讨论】:

    【解决方案3】:

    我遇到了同样的错误。 我所做的只是使用安全协议 https 而不是 http

    由于我的服务器在 Heroku 上,http 无法正常工作。

    【讨论】:

    • 这节省了一天
    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 2023-03-11
    • 2018-08-16
    • 2017-10-29
    • 2017-11-08
    • 1970-01-01
    • 2018-06-11
    相关资源
    最近更新 更多