我在尝试发布POST时遇到此错误。
状态响应:URL为200:null ** {" _body":{}," status":200," ok":true," statusText":" ","标题":{},"输入":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"
}
}
**** **** UPDATE 我尝试使用nativescript http组件来执行请求(https://docs.nativescript.org/cookbook/http),但在尝试解析为JSON时发生了其他错误:
未捕获(在承诺中):SyntaxError:位于0的JSON中的意外标记<
我在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);
答案 0 :(得分:1)
我也遇到了同样的错误。 我所做的就是使用安全协议 https 而不是 http
由于我的服务器位于Heroku上,因此http无法正常工作。
答案 1 :(得分:0)
尝试将地图更改为:
.map(result => result.json())
您可能需要根据数据的返回方式将数据映射到一个级别:
.map(result => result.json().data)
答案 2 :(得分:0)
我有同样的问题。我试图访问可通过localhost访问的Web服务。看起来这样在nativescript中不起作用。
解决方案:使用http://0.0.0.0
代替http://localhost
。