‘AxiosError’在升级到PHP 8.x和Laravel 9.x后出现’请求失败,状态码为500’的错误。

huangapple go评论55阅读模式
英文:

AxiosError 'Request failed with status code 500' after upgrading to PHP 8.x and Laravel 9.x

问题

  • 我试图升级项目的PHP版本到8.2以及Laravel版本到9.x。
  • 旧版本是PHP 7.3和Laravel 8.x。
    在升级之前,一切都按预期工作。
  • 升级到这些版本后,我遇到了一些API调用的问题。

问题目前只在本地出现,尚未尝试上传到服务器。
有时(但不总是),我项目中的某些随机API调用返回以下错误:

AxiosError {message: 'Request failed with status code 500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', config: {}, request: XMLHttpRequest, }
code: 
"ERR_BAD_RESPONSE"

config: 
{transitional: {}, adapter: Array(2), transformRequest: Array(1), transformResponse: Array(1), timeout: 0, }

message: 
"Request failed with status code 500"

name: 
"AxiosError"

request: 
XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, }

response: 
{data: '{
    "message": "Server Error"
}{
    "message": "Server Error"
}', status: 500, statusText: 'Internal Server Error', headers: AxiosHeaders, config: {}, }

stack: 
"AxiosError: Request failed with status code 500
    at settle (webpack-internal:///./node_modules/axios/lib/core/settle.js:24:12)
    at XMLHttpRequest.onloadend (webpack-internal:///./node_modules/axios/lib/adapters/xhr.js:120:66)"

[[Prototype]]: 
Error

这是我在控制台中收到的内容。网络标签只显示:“message”: “Server Error”。

我还想提一下:

  • 我的前端使用Vue 2,后端使用PHP/Laravel。
  • 我的API调用使用axios包。
  • 我使用Windows 10操作系统,并且使用XAMPP。
  • 我的IDE是Visual Studio Code(如果有关系的话)。

我知道我提到的错误可能不太明确,听起来只是一般的服务器错误,但是可能的原因是什么?

升级应该是正常的,我确保通过php -v命令使用了PHP 8.2。

我已调整了所有配置文件,例如php.inihttpd.conf等(如果需要,我可以上传它们)。

最后要提到的是,因为某种奇怪的原因,当我使用Xdebug来调试项目并在每个控制器函数的开头放置断点(API调用在后端的方向),错误从不出现。但这显然不是解决方案,因为我不想在所有后端函数中放置断点,并让Visual Studio弹出每个API调用。

编辑:查看Laravel日志后,我看到了以下信息:“production.ERROR: 未指定应用程序加密密钥。”经过查看,我找到了使用以下命令的解决方法:php artisan key:generatephp artisan config:cache。它们引发了不同的错误,我的项目的第一个API调用出现问题 - 在这些命令之后,.env文件不再可读,例如代码行“env('OKTA_CLIENT_ID')”现在返回null,而不是实际值。

任何帮助将不胜感激,谢谢。

英文:
  • I am trying to upgrade the version of my project's PHP to 8.2 and the version of my Laravel to 9.x.
  • The old versions were PHP 7.3 and Laravel 8.x.
    Before upgrading everything worked as expected.
  • After upgrading to the said versions I encountered a problem with some of my API Calls.

The problem is currently happening locally for me, didn't try to upload it to the server.
Sometimes (but not always), random API calls I have in my project are returning the following error:

AxiosError {message: 'Request failed with status code 500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', config: {…}, request: XMLHttpRequest, …}
code: 
"ERR_BAD_RESPONSE"

config: 
{transitional: {…}, adapter: Array(2), transformRequest: Array(1), transformResponse: Array(1), timeout: 0, …}

message: 
"Request failed with status code 500"

name: 
"AxiosError"

request: 
XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}

response: 
{data: '{\n    "message": "Server Error"\n}{\n    "message": "Server Error"\n}', status: 500, statusText: 'Internal Server Error', headers: AxiosHeaders, config: {…}, …}

stack: 
"AxiosError: Request failed with status code 500\n    at settle (webpack-internal:///./node_modules/axios/lib/core/settle.js:24:12)\n    at XMLHttpRequest.onloadend (webpack-internal:///./node_modules/axios/lib/adapters/xhr.js:120:66)"

[[Prototype]]: 
Error

This is what I receive in my console. The network tab just says: "message": "Server Error"

I'll mention:

  • I'm using Vue 2 as my frontend and PHP/Laravel as my backend.
  • My API calls are made with axios package.
  • I'm on a Windows 10 OS and I'm using XAMPP.
  • My IDE is Visual Studio Code if that matters.

I know the error I mentioned doesn't say too much probably and it sounds just like some general server error, but what could be the cause?

The upgrades did work fine, and I made sure I'm at PHP 8.2 with the command php -v.

I adjusted all the config files such as php.ini, httpd.conf, etc. (I can upload them if its needed).

One last thing I'd like to mention is that for some weird reason, when I use the Xdebug to debug my project and place breakpoints in the start of every controller function (the direction to where my API call goes in the backend) - The errors are never appearing. But this is obviously not a solution since I don't want to fill all my backend functions with breakpoints and have the Visual studio pop up every API call.

EDIT: After looking at the Laravel log I saw the following: "production.ERROR: No application encryption key has been specified.". After a look I found the solution to use the following commands: php artisan key:generate, php artisan config:cache. They caused a different error, the very first API call of my project bugs out - .env file became none readable anymore after the commands. a code row like: "env('OKTA_CLIENT_ID')" returns null now instead of the actual value.

Any help will be appreciated, Thanks.

答案1

得分: 0

SOLVED!

The error: production.ERROR: No application encryption key has been specified was solved by using the command: php artisan config:cache.

".env file became none readable anymore after the commands" was fixed with the command php artisan config:clear.

Afterwards, in order to fix the repeating issue every time I cleared the browser cache what I did was adding the 2 following lines to my UserController.php file, a part of that file is being called only when the user is a new user or when the configuration has been reseted. the lines are:

Artisan::call('config:clear');
Artisan::call('config:cache');

That way, those lines will only run after a browser cache clean or when a new user logs in the website, seemed to work perfectly and fix all the problems.

英文:

SOLVED!

The error: production.ERROR: No application encryption key has been specified was solved by using the command: php artisan config:cache.

".env file became none readable anymore after the commands" was fixed with the command php artisan config:clear.

Afterwards, in order to fix the repeating issue every time I cleared the browser cache what I did was adding the 2 following lines to my UserController.php file, a part of that file is being called only when the user is a new user or when the configuration has been reseted. the lines are:

Artisan::call('config:clear');
Artisan::call('config:cache');

That way, those lines will only run after a browser cache clean or when a new user logs in the website, seemed to work perfectly and fix all the problems.

huangapple
  • 本文由 发表于 2023年4月4日 17:22:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定