gpt4 book ai didi

javascript - ionic2/3 获取和发布不工作

转载 作者:行者123 更新时间:2023-11-30 20:29:11 26 4
gpt4 key购买 nike

我是 ionic2 的新手(但总体上是编程的老手),正在尝试 ionic2。现在,我正在尝试对我的 Web 服务(在开发中本地运行)进行简单的获取/发布,但我不能只获取调用的服务。

有趣的是,get 和 post 调用不会导致任何错误/警告/任何其他情况。


import { Injectable, NgZone } from '@angular/core';

import { BackgroundGeolocation } from '@ionic-native/background-geolocation';

import { Geolocation, Geoposition } from '@ionic-native/geolocation';

import { Http, Response, Headers, RequestOptions } from '@angular/http';

import { Observable } from 'rxjs';

import 'rxjs/add/operator/map';

import 'rxjs/add/operator/catch';

import 'rxjs/add/operator/toPromise';import 'rxjs/add/operator/filter';

@Injectable()
export class LocationTracker {
public watch: any;
public lat: number = 0;
public lng: number = 0;
public http: Http;

constructor(public zone: NgZone, public backgroundGeolocation: BackgroundGeolocation,
public geolocation: Geolocation, http: Http) {
this.http = http;
}

public postLocationData(lat: number, lng: number) {
var JSONObject = {"latitude": "" + lat, "longitude": "" + lng };

console.log("Posting: " + JSON.stringify(JSONObject));
let headerOptions: any = { 'Content-Type': 'application/json' };
let headers = new Headers(headerOptions);

// The simple get service. Curl from command line works
this.http.get("http://192.168.1.2:8080/location/new1/")
.map((response: Response) => {
console.log('Response is: ' + response);
})
.catch((this.handleErrorObservable));

// The simple post service. Curl from command line works. Note that in the service, the post param is captured as a String (Java) and then I am casting the string to an Object using Gson
this.http.post("http://192.168.1.2:8080/location/new/", JSON.stringify(JSONObject), new RequestOptions({ headers: headers }))
.map((response: Response) => {
console.log('Response is: ' + response);
})
.catch((this.handleErrorObservable));
}

startTracking() {
// lat and lng are populated here
}

stopTracking() {
// something ...
}
private handleErrorObservable (error: Response | any) {
console.error('Error in handleErrorObservable: ' + ( error.message || error ) );
return Observable.throw(error.message || error);
}
}

类似的片段也出现在我发布的 pastebin 中 ( https://pastebin.com/f39vW1hD )。非常感谢任何帮助。

最佳答案

没有订阅或异步管道,您无法看到发送的请求。

public postLocationData(lat: number, lng: number)  {
var JSONObject = {"latitude": "" + lat, "longitude": "" + lng };

console.log("Posting: " + JSON.stringify(JSONObject));
let headerOptions: any = { 'Content-Type': 'application/json' };
let headers = new Headers(headerOptions);

// The simple get service. Curl from command line works
this.http.get("http://192.168.1.2:8080/location/new1/")
.map((response: Response) => {
console.log('Response is: ' + response);
})
.subscribe(data => console.log(data));

// The simple post service. Curl from command line works. Note that in the service, the post param is captured as a String (Java) and then I am casting the string to an Object using Gson
this.http.post("http://192.168.1.2:8080/location/new/", JSON.stringify(JSONObject), new RequestOptions({ headers: headers }))
.map((response: Response) => {
console.log('Response is: ' + response);
})
.subscribe(data => console.log(data));
}

关于javascript - ionic2/3 获取和发布不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50551813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com