Laravel项目提交数据遇500内部服务器错误(CSRF已配置)
Hey there, let's dig into your 500 error issue—looks like there are a couple of clear mismatches between your AJAX data, controller logic, and likely your Post model that's causing the problem. Let's break it down step by step:
1. Mismatched Request Parameter Names
Your AJAX request sends parameters named title, body, and author, but in your controller, you're trying to fetch createTitle, createPost, and createAuthor. Laravel can't find these unpassed parameters, which is almost certainly triggering a validation or undefined value error under the hood.
2. Model & Database Field Mismatch
You mentioned your Posts table has fields Title, Post body, and author. In your controller, you're assigning $post->post—but your table field is likely post_body (since database tables don't typically use spaces) or body, not post. This mismatch will cause a database error when trying to save the record.
3. Missing Fillable Attributes in Post Model
Laravel's Eloquent protects against mass assignment by default. If your Post model doesn't explicitly list the fields you're trying to save in the $fillable array, it will throw a MassAssignmentException—another common cause of 500 errors.
Fixed Code Snippets
Updated Controller Code
public function create(Request $request){ // First, validate incoming data to catch invalid inputs early $validated = $request->validate([ 'title' => 'required|string|max:255', 'body' => 'required|string', 'author' => 'required|string|max:255' ]); $post = new Post; // Match parameter names to what AJAX sends, and align with your actual table fields $post->title = $request->input('title'); // Replace 'body' with 'post_body' here if your table uses that field name $post->body = $request->input('body'); $post->author = $request->input('author'); $post->save(); return response()->json([ 'response' => 'successfully added', 'error' => null // Set error to null on success for clarity ]); }
Updated Post Model (app/Models/Post.php)
namespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model { // List all fields you want to allow for mass assignment protected $fillable = ['title', 'body', 'author']; // Uncomment below if your table name is explicitly 'Posts' (Laravel defaults to lowercase plural 'posts') // protected $table = 'Posts'; }
Improved AJAX Error/Success Handling
Your original code had a typo in the HTML tag you're appending—fix that to properly display feedback:
success:function(response){ $("#response").append("<div>" + response.response + "</div>"); }, error: function(error){ // Fetch detailed error message from Laravel's JSON response const errorMsg = error.responseJSON?.message || 'Unknown server error'; $("#response").append("<div>Error: " + errorMsg + "</div>"); }
Debugging Tip
Always check Laravel's log file at storage/logs/laravel.log when you get a 500 error—it will show you the exact line and error type (e.g., missing field, mass assignment issue) that's causing the problem. This is the fastest way to pinpoint Laravel server errors.
内容的提问来源于stack exchange,提问作者I Love Code

