首页 > 解决方案 > 从 Angular 发送到 PHP 的 POST 对象为空,Get 和 Server 正在响应

问题描述

我已经被困在这几天了。我尝试通过 HttpClient 模块从 Angular 使用站点,到目前为止,我已经通过标头传递了 CORS,并且服务器响应了我的请求,但是我需要通过邮寄方式发送数据,如果我打印服务器响应它会打印 GET, REQUEST 和 SERVER 没有问题,但 POST 它作为空对象返回。我已经查看了一些文档并测试了一些配置,但我没有成功。有没有人遇到过类似的情况?

我的帖子数据

  postData = {
    usuario: 'admin@localhost.com',
    password: 'securePassword',
    texto: 'This is a Simple Text'
  };

我的角函数

  testConection(postData) {

this.http.post(
  this.url,
  postData,
{responseType: 'text'} /* {responseType: 'json'} */
)
.toPromise()
.then(
  response => {
    console.log(response);
  }
);

我的PHP函数代码

  public function post()
    {
        $response['POST'] = $_POST;
        $response['GET'] = $_GET;
        $response['SERVER'] = $_SERVER;
        $response['REQUEST'] = $_REQUEST;
        $response['COOKIE'] = $_COOKIE;
        $response['ENV'] = $_ENV;
        
        // echo json_encode($response);

        print_r($response);

PHP 和 .htaccess 中的标头

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Allow: GET, POST, OPTIONS, PUT, DELETE");

在控制台中响应。 在控制台中响应

标签: phpangular.htaccesscodeigniter-3

解决方案


推荐阅读