![](/img/trans.png)
[英]POST http://localhost:3000/upload 500 (Internal Server Error)
[英]how to fix POST http://localhost:8000/cour 500 (Internal Server Error) laravel,ajax and Yajra pakage
我该如何解决这个问题,我正在使用 package yajra 但我不知道问题出在哪里。
我收到 500(内部服务器错误)
<?php
namespace App\Http\Controllers;
use App\Cour;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
//use Validator;
class CourController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if(request()->ajax())
{
return datatables()->of(Cour::latest()->get())
->addColumn('action', function($data){
$button = '<button type="button" name="edit" id="'.$data->id.'" class="edit btn btn-primary btn-sm">Edit</button>';
$button .= ' ';
$button .= '<button type="button" name="delete" id="'.$data->id.'" class="delete btn btn-danger btn-sm">Delete</button>';
return $button;
})
->rawColumns(['action'])
->make(true);
}
return view('cour');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$rules = array(
'prof' => 'required',
'module' => 'required',
'chapitre' => 'required',
'titre' => 'required',
'description' => 'required',
'file' => 'required|file|max:5000|mimes:pdf,docx,doc',
);
$error = Validator::make($request->all(), $rules);
if($error->fails())
{
return response()->json(['errors' => $error->errors()->all()]);
}
$file = $request->file('file');
$new_name = rand() . '.' . $file->getClientOriginalExtension();
$file->move(public_path('images'), $new_name);
$form_data = array(
'prof' => $request->prof,
'module' => $request->module,
'chapitre' => $request->chapitre,
'titre' => $request->titre,
'description' => $request->description,
'file' => $new_name
);
Cour::create($form_data);
return response()->json(['success' => 'Data Added successfully.']);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
if(request()->ajax())
{
$data = Cour::findOrFail($id);
return response()->json(['data' => $data]);
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
$file_name = $request->hidden_file;
$file = $request->file('file');
if($file != '')
{
$rules = array(
'prof' => 'required',
'module' => 'required',
'chapitre' => 'required',
'titre' => 'required',
'description' => 'required',
'file' => 'required|file|mimes:pdf,docx,doc|max:5000',
);
$error = Validator::make($request->all(), $rules);
if($error->fails())
{
return response()->json(['errors' => $error->errors()->all()]);
}
$file_name = rand() . '.' . $file->getClientOriginalExtension();
$file->move(public_path('images'), $file_name);
}
else
{
$rules = array(
'prof' => 'required',
'module' => 'required',
'chapitre' => 'required',
'titre' => 'required',
'description' => 'required',
);
$error = Validator::make($request->all(), $rules);
if($error->fails())
{
return response()->json(['errors' => $error->errors()->all()]);
}
}
$form_data = array(
'prof' => $request->prof,
'module' => $request->module,
'chapitre' => $request->chapitre,
'titre' => $request->titre,
'description' => $request->description,
'file' => $file_name
);
Cour::whereId($request->hidden_id)->update($form_data);
return response()->json(['success' => 'Data is successfully updated']);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$data = Cour::findOrFail($id);
$data->delete();
}
public function download($file)
{
return response()->download('storage/images'.$file);
}
}
观点
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 5.8 - DataTables Server Side Processing using Ajax</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<br />
<h3 align="center">Laravel 5.8 Ajax Crud Tutorial - Delete or Remove Data</h3>
<br />
<div align="right">
<button type="button" name="create_record" id="create_record" class="btn btn-success btn-sm">Create Record</button>
</div>
<br />
<div class="table-responsive">
<table class="table table-bordered table-striped" id="user_table">
<thead>
<tr>
<th width="">Id</th>
<th width=""><i class="icon_profile"></i>professeur</th>
<th width="">module</th>
<th width="">chapitre</th>
<th width="">titre</th>
<th width="">description</th>
<th width="30%">Action</th>
</tr>
</thead>
</table>
</div>
<br />
<br />
</div>
</body>
</html>
<div id="formModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add cour</h4>
</div>
<div class="modal-body">
<span id="form_result"></span>
<form method="post" id="sample_form" class="form-horizontal" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label class="col-md-4 text-right">Enseignant:</label>
<div class="col-md-8">
<input type="text" name="prof" class="form-control input-lg" placeholder="le nom du prof" />
</div>
</div>
<div class="form-group">
<label class="col-md-4 text-right">Module:</label>
<div class="col-md-8">
<input type="text" name="module" class="form-control input-lg" placeholder="module"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 text-right">Chapitre:</label>
<div class="col-md-8">
<input type="text" name="chapitre" class="form-control input-lg" placeholder="chapitre"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 text-right">Titre:</label>
<div class="col-md-8">
<input type="text" name="titre" class="form-control input-lg" placeholder="titre"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 text-right">Description:</label>
<div class="col-md-8">
<input type="text" name="description" class="form-control input-lg" placeholder="description"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 text-right">choisir le fichier</label>
<div class="col-md-8">
<input type="file" name="file" id="file" />
<span id="store_image"></span>
</div>
</div>
<div class="form-group" align="center">
<input type="hidden" name="action" id="action" />
<input type="hidden" name="hidden_id" id="hidden_id" />
<input type="submit" name="action_button" id="action_button" class="btn btn-warning" value="Add" />
</div>
</form>
</div>
</div>
</div>
</div>
<div id="confirmModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h2 class="modal-title">Confirmation</h2>
</div>
<div class="modal-body">
<h4 align="center" style="margin:0;">Are you sure you want to remove this data?</h4>
</div>
<div class="modal-footer">
<button type="button" name="ok_button" id="ok_button" class="btn btn-danger">OK</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$('#user_table').DataTable({
processing: true,
serverSide: true,
ajax:{
url: "{{ route('cour.index') }}",
},
columns:[
{
data: 'file',
name: 'file',
render: function(data, type, full, meta){
return "<img src={{ URL::to('/') }}/images/" + data + " width='70' class='img-thumbnail' />";
},
orderable: false
},
{
data: 'prof',
name: 'prof'
},
{
data: 'module',
name: 'module'
},
{
data: 'chapitre',
name: 'chapitre'
},
{
data: 'titre',
name: 'titre'
},
{
data: 'description',
name: 'description'
},
{
data: 'action',
name: 'action',
orderable: false
}
]
});
$('#create_record').click(function(){
$('.modal-title').text("Ajouter un coure");
$('#action_button').val("Add");
$('#action').val("Add");
$('#formModal').modal('show');
});
$('#sample_form').on('submit', function(event){
event.preventDefault();
if($('#action').val() == 'Add')
{
$.ajax({
url:"{{ route('cour.store') }}",
method:"POST",
data: new FormData(this),
contentType: false,
cache:false,
processData: false,
dataType:"json",
success:function(data)
{
var html = '';
if(data.errors)
{
html = '<div class="alert alert-danger">';
for(var count = 0; count < data.errors.length; count++)
{
html += '<p>' + data.errors[count] + '</p>';
}
html += '</div>';
}
if(data.success)
{
html = '<div class="alert alert-success">' + data.success + '</div>';
$('#sample_form')[0].reset();
$('#user_table').DataTable().ajax.reload();
}
$('#form_result').html(html);
}
})
}
if($('#action').val() == "Edit")
{
$.ajax({
url:"{{ route('cour.update') }}",
method:"POST",
data:new FormData(this),
contentType: false,
cache: false,
processData: false,
dataType:"json",
success:function(data)
{
var html = '';
if(data.errors)
{
html = '<div class="alert alert-danger">';
for(var count = 0; count < data.errors.length; count++)
{
html += '<p>' + data.errors[count] + '</p>';
}
html += '</div>';
}
if(data.success)
{
html = '<div class="alert alert-success">' + data.success + '</div>';
$('#sample_form')[0].reset();
$('#store_image').html('');
$('#user_table').DataTable().ajax.reload();
}
$('#form_result').html(html);
}
});
}
});
$(document).on('click', '.edit', function(){
var id = $(this).attr('id');
$('#form_result').html('');
$.ajax({
url:"/cour/"+id+"/edit",
dataType:"json",
success:function(html){
$('#prof').val(html.data.prof);
$('#module').val(html.data.module);
$('#chapitre').val(html.data.chapitre);
$('#titre').val(html.data.titre);
$('#description').val(html.data.description);
$('#store_file').html("<iframe src={{ URL::to('/') }}/images/" + html.data.file + " width='70' class='img-thumbnail' ></iframe>");
$('#store_file').append("<input type='hidden' name='hidden_file' value='"+html.data.file+"' />");
$('#hidden_id').val(html.data.id);
$('.modal-title').text("Edit New Record");
$('#action_button').val("Edit");
$('#action').val("Edit");
$('#formModal').modal('show');
}
})
});
var user_id;
$(document).on('click', '.delete', function(){
user_id = $(this).attr('id');
$('#confirmModal').modal('show');
});
$('#ok_button').click(function(){
$.ajax({
url:"cour/destroy/"+user_id,
beforeSend:function(){
$('#ok_button').text('Deleting...');
},
success:function(data)
{
setTimeout(function(){
$('#confirmModal').modal('hide');
$('#user_table').DataTable().ajax.reload();
}, 2000);
}
})
});
});
</script>
路线:
Route::resource('cour', 'CourController');
Route::post('cour/update', 'CourController@update')->name('cour.update');
Route::get('cour/destroy/{id}', 'CourController@destroy');
当你得到 500 错误? 如果您的数据表发送 ajax 时收到,您可以尝试在发送前将 CSRF 添加到数据表的 header 请求中。 此外,您可以更改 your.env APP_DEBUG=true 以查看有关您的错误的更多信息。 当您想从此发送 ajax 时的示例
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
您可以在此处查看如何将 csrf 添加到 header 数据表请求
"ajax":{
url: url,
type: "GET",
headers: { 'Apitoken': apiToken },
});
您应该在 header 的元标记中设置您的 csrf
<meta name="csrf-token" content="{{ csrf_token() }}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.