javascript - 无法使用 http.post 离子发送数据
问题描述
我无法使用以下代码通过 http.post 发送数据。我能够得到响应,但无法发送数据进行处理。
以下是代码:
应用服务器.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
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) 返回一个空值/
解决方案
http.post 接受三个参数
1)网址,
2)您要发送的数据,
3)标题
在您指定为 json 但您正在执行 JSON.stringify 的标头中,它会将其转换为字符串,这可能是错误。按原样发送可能会解决问题。
尝试这个
this.http.post('http://www.o9village.com/myBOT_api/botfeeds.php', req, headers)
推荐阅读
- java - 社交柜台查询
- docker - 数据库“telegraf”创建失败:发布“http://influxdb:8086/query”:拨打 tcp 172.31.0.2:8086:连接:连接被拒绝
- django - 依赖于另一个应用程序模型的两个独立应用程序模型上的 Django 循环依赖错误
- javascript - 使用 JavaScript 从 Google Chrome 扩展程序读取数据
- javascript - 如何从另一个变量访问定义的变量,其中内容是定义变量的名称(java脚本)
- javascript - 需要帮助修改进度条 - html/jquery
- python - Python 找不到 Blender 模块
- python - Discord.py 连接调用失败('::1',9000),[Errno 10061] 连接调用失败('127.0.0.1',9000)
- user-interface - 领事用户界面不显示
- python - 使用 sklearn 宏 f1-score 作为 tensorflow.keras 中的指标