使用 Laravel 和 ajax 的内部服务器错误
Posted
技术标签:
【中文标题】使用 Laravel 和 ajax 的内部服务器错误【英文标题】:internal server error using Laravel and ajax 【发布时间】:2020-12-20 14:38:45 【问题描述】:我是 js 和 ajax 的初学者,我试图在 Wamp 本地服务器上的全新 Laravel 项目上做一些关于 ajax 的基本教程。
我每次尝试都收到内部服务器错误 500,在反复检查路由或令牌后我有点不知所措。
作为参考,我使用了这个链接上的基本示例Minimum Working Example for ajax POST in Laravel 5.3
有人可以就我应该检查的内容提供一些指导吗?
谢谢
编辑:我的错误:我没有注意控制器中的语法,这是我的错误。非常感谢 Felipe Duarte,他让我查看 laravel.log。错误出现在第 1 行。
拉拉维尔。日志
#29 C:\wamp64\www\laravel_test\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#30 C:\wamp64\www\laravel_test\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#31 C:\wamp64\www\laravel_test\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#32 main
路线:
Route::get('ajax', function() return view('ajax'); );
Route::post('/postajax','AjaxController@post');
控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
public function post(Request $request)
$response = array(
'status' => 'success',
'msg' => $request->message,
);
return response()->json($response); //
观点:
<!DOCTYPE html>
<html>
<head>
<!-- load jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- provide the csrf token -->
<meta name="csrf-token" content=" csrf_token() " />
<script>
$(document).ready(function()
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$(".postbutton").click(function()
$.ajax(
/* the route pointing to the post function */
url: 'postajax',
type: 'POST',
/* send the csrf-token and the input to the controller */
data: _token: CSRF_TOKEN, message:$(".getinfo").val(),
dataType: 'JSON',
/* remind that 'data' is the response of the AjaxController */
success: function (data)
$(".writeinfo").append(data.msg);
);
);
);
</script>
</head>
<body>
<input class="getinfo"></input>
<button class="postbutton">Post via ajax!</button>
<div class="writeinfo"></div>
</body>
</html>
【问题讨论】:
错误是什么?看storage/logs/laravel.log
或者apache error_log
我用从日志中得到的信息更新了我的帖子
看到以#29
开头的那一行了吗?表示堆栈错误的第 29 行。使用此错误的所有行和错误消息更新您的问题。
非常感谢费利佩。谢谢你,我发现了错误。我忘记了控制器中的 。我真是个菜鸟。
没问题,总是先查看错误日志。通常是一件简单的事情。祝你好运。
【参考方案1】:
这里:
Route::get('ajax', function() return view('ajax'); );
Route::post('/postajax','AjaxController@post'); //you can add name to this route
您可以将 ajax 数据中的 _token 替换为 HTTP 标头,将其放在 ajax() 之前
$.ajaxSetup(
headers:
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
);
那么你的 ajax 应该是:
$.ajax(
/* the route pointing to the post function */
url: 'postajax', // change to url('/postajax') or with route_name route('route_name') in blade format
type: 'POST',
/* send the csrf-token and the input to the controller */
data: _token: CSRF_TOKEN, message:$(".getinfo").val(),
dataType: 'JSON',
/* remind that 'data' is the response of the AjaxController */
success: function (data)
$(".writeinfo").append(data.msg);
);
【讨论】:
以上是关于使用 Laravel 和 ajax 的内部服务器错误的主要内容,如果未能解决你的问题,请参考以下文章
在 laravel 5.2 中使用 jquery 和 ajax 出现 500 内部服务器错误
500(内部服务器错误)在 Laravel 5 中使用 Ajax
如何修复 Laravel 5.8 Ajax 表单提交中的内部服务器错误
Laravel 5.7 ajax 请求 500(内部服务器错误)
Laravel 4 内部服务器错误 500 和 HTML 表中的 TokenMismatchException 以使用 ajax 更新数据库