我有问题,当我将数据发布到我的api服务器时。
我的api服务器没有获得任何数据,它是从离子应用程序发送的。
这是我的api提供商。
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)中,我尝试vardump($_POST);
/但它返回array(0) {}
控制台日志
[15:20:15] console.log: 200
[15:20:15] console.log: array(0) { }
[15:20:15] console.log: [object Object]
系统信息:
答案 0 :(得分:2)
刚刚将"Content-Type": "application/json"
更改为"Content-Type": "application/x-www-form-urlencoded"
。
答案 1 :(得分:0)
尝试使用' @ angular / http'。
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);
});
});