[使用php的Angular 7 http发布失败,并显示404

问题描述 投票:0回答:1

对于我的网站(使用Angular 7编程),我建立了一个联系表单,用于将JSON对象发送到PHP文件。

Screenshot console error

MessageService将绝对路径传递到PHP文件和POST中的消息对象。 POST可以正常运行,但是我却收到了404。

message.service.ts

export class MessageService {
  baseUrl = '<domain>/dev/src/app/entities/contact/message.php';

  constructor(private http: HttpClient) { }

  send(message: Message) {
    this.http.post(this.baseUrl, { data: message }).subscribe((data) => {
      console.log('Sent Data', data);
    }, (error) => {
      console.log('Something went wrong', error);
    });
  }
}

message.php

<?php
  header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
  header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
  header('Access-Control-Max-Age: 1000');
  header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

  $post = file_get_contents('php://input');

  echo $postdata;
?>

MessageService和message.php位于同一文件夹中。但是我不确定POST中的绝对路径是否正确。

Screenshot file tree

如果您能帮助我解决我的问题,我会很高兴。

非常感谢

Sascha

angular forms typescript post http-post
1个回答
0
投票

尝试使用header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_REFERER']);header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);

某些浏览器未设置HTTP_ORIGIN,因此使用起来不安全


0
投票

我已添加

# redirect from http to https
RewriteEngine on
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

# Header add Access-Control-Allow-Origin "*"
# Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
# Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

进入htaccess,但这没有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.