【发布时间】:2018-12-01 17:53:46
【问题描述】:
我的主机上的 POST 请求有问题。在我的托管服务器上,我有域。我们可以假设它是:http://example.com
有两个应用程序:Angular 6 作为前端,laravel 5.6 作为后端。在 public_html 文件夹中构建了 angular 应用程序,但在子文件夹 /api 中我放置了 laravel rest API 应用程序。
有 .httaccess 文件应该将 /api/* 请求重定向到 laravel 应用程序的 public.html。它看起来像:
DirectoryIndex index.html index.php
AuthType Basic
AuthName "Wstep wzbroniony"
AuthUserFile /home/Smartap/domains/smartap.usermd.net/public_html/.htpasswd
AuthGroupFile /dev/null
require valid-user
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/api/
RewriteRule (.*) /api/apeczka/public/index.php [L]
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
当我在浏览器链接中过去时它可以正常工作:http://example.comapi/test
我从 API 的 rest route 获取测试数据。但问题是当 Angular 应用程序向 API 发送 POST 请求以登录用户时。它不起作用。我得到:
{
"message": "Server Error"
}
来自 laravel 后端。我应该如何设置它?在 localhost 上一切正常,在那里我有两个不同的服务器:localhost:4200 - 用于 angular,localhost:8004 - 用于 laravel。
编辑: 在服务器日志中,我发现了类似的内容:
[2018-12-01 20:18:13] production.ERROR: SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO) {"exception":"[object] (Doctrine\\DBAL\\Driver\\PDOException(code: 1045): SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO) at /usr/home/Smartap/domains/smartap.usermd.net/public_html/api/apeczka/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:50, PDOException(code: 1045): SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO) at /usr/home/Smartap/domains/smartap.usermd.net/public_html/api/apeczka/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:46)
[stacktrace]
我很乐意提供帮助。
最好的问候。
【问题讨论】:
-
JSON 格式的错误看起来像是来自您的 Laravel API,所以您的 .htaccess 工作正常。你需要检查你的服务器的错误日志,看看是否有任何 PHP 错误可以解释为什么 Laravel 会出现服务器错误。
-
谢谢我已经从 laravel 粘贴了日志。你能看看他们吗?
-
这可能有帮助吗? stackoverflow.com/a/42261922
-
好的,问题已经解决了。我在服务器上没有 .env 文件。我不知道为什么;)
标签: php angular laravel .htaccess web-hosting