【问题标题】:Angular 5 and PHP Cannot POST to a server side fileAngular 5 和 PHP 无法 POST 到服务器端文件
【发布时间】:2018-10-19 12:20:02
【问题描述】:

出于某些测试目的,我尝试将加密变量从 angular 发送到 php 脚本。

这是客户端脚本:

ngOnInit(){
let user = "am";
let key = "pizza";
let enc = crypto.AES.encrypt(user, key);
console.log(enc);
let dec = crypto.AES.decrypt(enc, key);
console.log(dec.toString(crypto.enc.Utf8));

const headerOptions = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
const params = new HttpParams().set('name', enc)
this.http.post('aff.local/test.php', params, {
    headers: headerOptions
}).subscribe(
        res=>{
            console.log(res)
        },
        error=>{
            console.log( );
        }
    )
}

和 test.php 脚本:

<?php
error_reporting(E_ALL);
ini_set('log_errors', 0);
ini_set('display_errors', 1);

$host = "localhost";
$db = "dentist";
$user = "root";
$pass = "root";

$conn = new PDO("mysql:host={$host};dbname={$db};", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET CHARACTER SET utf8mb4");
$enc = $_POST['name'];
echo $enc;
?>

我在响应中有以下错误:

POST http://localhost:4200/aff.local/test.php 404(未找到)

在网络标签中:

Cannot POST /aff.local/test.php

该文件确实存在于虚拟主机文件夹中。

【问题讨论】:

  • 您提供的 PHP 中没有任何内容会导致它输出 404。我们无法知道您的 HTTP 服务器为什么响应该 URL 的 404 错误。
  • 那么,我们能提供什么帮助呢?端口可能有误,aff.local 的位置可能丢失,......
  • 我需要发送给aff.local/test.php而不是http://localhost:4200/aff.local/test.php
  • 你能通过地址栏访问你的文件(url)吗?
  • 完成。解决方案:http://aff.local/test.php 而不是aff.local/test.php

标签: php angular angular5 vhosts


【解决方案1】:

试试这个。

POST http://localhost: 4200 / aff.local / test.php 404 (non trouvé)

**用服务器 php 替换服务器 angular(localhost:4200)(例如:wamp: localhost:8080/文件夹名/文件名)

localhost:8080 指定 wamp server par defaut 的目录 www。

【讨论】:

    【解决方案2】:

    我自己找到了解决办法。

    如果您只使用aff.local/test.php,Angular 会自动将其视为localhost:4200/yourlink。 只需添加http://yourlink 即可修复。

    所以解决方案是http://aff.local/test.php 而不是aff.local/test.php:

    ngOnInit(){
       let user = "am";
       let key = "pizza";
       let enc = crypto.AES.encrypt(user, key);
       console.log(enc);
       let dec = crypto.AES.decrypt(enc, key);
       console.log(dec.toString(crypto.enc.Utf8));
    
       const headerOptions = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
       const params = new HttpParams().set('name', enc)
       this.http.post('http://aff.local/test.php', params, {
           headers: headerOptions
      }).subscribe(
            res=>{
                console.log(res)
            },
            error=>{
                console.log( );
            }
        )
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多