我正在尝试使用HttpClient Angular向服务器发送POST请求。我的代码有误:
文件* .ts
import { HttpClient } from '@angular/common/http';
constructor(public http: HttpClient){}
public link ='http://opencart-ir.com/test/json.php';
public postData(){
let postData = new FormData();
postData.append('parentid','0');
this.http.post(this.link,this.postData)
.subscribe(data =>{
console.log(data);
}, error => {
console.log("Oooops!");
});
}
文件* .html
<button (click)="postData()">Post</button>
文件json.php
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$data= array(
'get' => $_GET,
'post' => $_POST
);
echo json_encode($data);
?>
在控制台中运行按摩
{get: Array(0), post: Array(0)}
get: []
post: []
__proto__: Object
答案 0 :(得分:1)
您要使用postData函数而不是变量,它应该是postData
而不是this.postData
:
this.http.post(this.link, postData).subscribe(data =>{
console.log(data);
}, error => {
console.log("Oooops!");
});