简体   繁体   English

无法使用http.post ionic发送数据

[英]not able to send data with http.post ionic

I'm not able to send data through http.post with the following code. 我无法使用以下代码通过http.post发送数据。 I'm able to get a response but not able to send data for processing. 我能够得到回应,但无法发送数据进行处理。

the following is the code: 以下是代码:

appServer.ts appServer.ts

import { Component } from '@angular/core';
import { Http, HttpModule, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'server',
  templateUrl: 'home.html'
})
export class appServer {
    connectServer: any;
    send_data: any;
    constructor(private http: Http) {
    }
    serverConnect(){
        let headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'});
        let req = {call: 'insert_BOT',data: this.send_data};
        this.http.post('http://www.o9village.com/myBOT_api/botfeeds.php',JSON.stringify(req), headers)
        .map(res=>res.json())
        .subscribe(res=>{
            this.connectServer = res;
            console.log(res);
        },(err)=>{
            console.log('Can\'t fetch data');
        })
    }
}

botfeeds.php botfeeds.php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');

echo json_encode($array);

console.log(res) returns a null value/ console.log(res)返回一个空值/

You can not send post data ie when you send data with 您无法发送帖子数据,即当您使用

Header Content-Type: application/json you can not see it in $_POST variable in php. 标头Content-Type:application / json,您无法在php的$ _POST变量中看到它。

You have to send data with 您必须使用

Header Content-Type: application/x-www-form-urlencoded 标头内容类型:application / x-www-form-urlencoded

Then you will be able to see Post data on server. 然后,您将能够看到在服务器上发布数据。

http.post takes three parameters http.post具有三个参数

1) url, 1)网址,

2) data you want to send, 2)您要发送的数据,

3)header 3)标题

in the header you specify as json but you are doing JSON.stringify it will convert it into string may be this is the error. 在您指定为json的标头中,但是您正在执行JSON.stringify,它将把它转换成字符串,这可能是错误的。 send it as it is may solve the problem. 照原样发送可能会解决问题。

try this 尝试这个

this.http.post('http://www.o9village.com/myBOT_api/botfeeds.php', req, headers)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM