Ionic 2 HTTP.post not send data
我将数据发布到api服务器时遇到问题。
我的api服务器未获取任何数据,而是从ionic应用程序发送的。
这是我的api提供程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import { Injectable } from '@angular/core'; import { HTTP } from 'ionic-native'; import 'rxjs/add/operator/map'; @Injectable() export class APIProvider { constructor() {} getProduct(){ return new Promise(resolve => { let header = {"Content-Type":"application/json"} let body = { "data_1":"1234567890123", "data_2":"125615" } HTTP.post('https://ex.com/api/somethig', JSON.stringify(body), header) .then(data => { console.log(data.status); console.log(data.data); // data received by server console.log(data.headers); resolve(data); }) .catch(error => { console.log(error.status); console.log(error.error); // error message as string console.log(error.headers); resolve(error); }); }); } } |
在api服务器(PHP)中,我尝试
控制台日志
1 2 3 | [15:20:15] console.log: 200 [15:20:15] console.log: array(0) { } [15:20:15] console.log: [object Object] |
系统信息:
- 科尔多瓦CLI:6.5.0
- 离子框架版本:2.0.0
- Ionic CLI版本:2.2.1
- Ionic App Lib版本:2.2.0
- Ionic应用程序脚本版本:1.0.0
- ios-deploy版本:未安装
- ios-sim版本:未安装
- 操作系统:macOS Sierra
- 节点版本:v6.9.5
- Xcode版本:Xcode 8.2.1 Build版本8C1002
啊,我找到了答案。
只需将
尝试使用" @ angular / http"。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import { Http, Headers } from '@angular/http'; constructor(public http: Http) {} let body = { data_1:"1234567890123", data_2:"125615" }; return new Promise((resolve, reject) => { let headers = new Headers(); headers.append('Content-Type', 'application/json'); this.http.post('https://ex.com/api/somethig', JSON.stringify(body), {headers: headers}) .subscribe(res => { resolve(res); }, (err) => { reject(err); }); }); |