Ajax laravel内部错误,有什么问题?
当我尝试使用ajax提交表单时,我得到了500(内部服务器错误)错误 谁能告诉我我做错了什么? 这是我的ajax、路由和控制器代码 AjaxAjax laravel内部错误,有什么问题?,ajax,laravel,Ajax,Laravel,当我尝试使用ajax提交表单时,我得到了500(内部服务器错误)错误 谁能告诉我我做错了什么? 这是我的ajax、路由和控制器代码 Ajax $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $("#subscribe_form").submit(function(event){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$("#subscribe_form").submit(function(event){
event.preventDefault();
console.log('a');
$.ajax({
url: "subscribe/store",
type: 'post',
//dataType: JSON,
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
beforeSend: function () {
},
success: function () {
console.log('success');
}
});
});
路线
Route::post('subscribe/store', 'SubscribeController@store')
->name('subscribe.store');
$('body').on('submit','#subscribe_form',function(event){
event.preventDefault();
console.log('a');
$.ajax({
url : $(this).attr('action'),
data: new FormData(this),
type: 'POST',
contentType: false,
cache: false,
processData:false,
beforeSend: function () {
},
success: function () {
console.log('success');
}
});
});
<form id="subscribe_form" style="margin-bottom: 5px" action="{{ route('subscribe.store') }}">
@csrf
<input type="email" name="email" placeholder="Enter Your Email" id="subemail">
<button type="submit" class="btn btn-dark sub" id="subscribe">Subscribe</button>
</form>
控制器
public function store(Request $request)
{
Subscriber::create($request->all());
return redirect()->route('index');
}
表格
<form id="subscribe_form" style="margin-bottom: 5px" >
@csrf
<input type="email" name="email" placeholder="Enter Your Email" id="subemail">
<button type="submit" class="btn btn-dark sub" id="subscribe">Subscribe</button>
</form>
@csrf
订阅
您可以像这样更改ajax和表单代码吗
Ajax代码
Route::post('subscribe/store', 'SubscribeController@store')
->name('subscribe.store');
$('body').on('submit','#subscribe_form',function(event){
event.preventDefault();
console.log('a');
$.ajax({
url : $(this).attr('action'),
data: new FormData(this),
type: 'POST',
contentType: false,
cache: false,
processData:false,
beforeSend: function () {
},
success: function () {
console.log('success');
}
});
});
<form id="subscribe_form" style="margin-bottom: 5px" action="{{ route('subscribe.store') }}">
@csrf
<input type="email" name="email" placeholder="Enter Your Email" id="subemail">
<button type="submit" class="btn btn-dark sub" id="subscribe">Subscribe</button>
</form>
表格代码
Route::post('subscribe/store', 'SubscribeController@store')
->name('subscribe.store');
$('body').on('submit','#subscribe_form',function(event){
event.preventDefault();
console.log('a');
$.ajax({
url : $(this).attr('action'),
data: new FormData(this),
type: 'POST',
contentType: false,
cache: false,
processData:false,
beforeSend: function () {
},
success: function () {
console.log('success');
}
});
});
<form id="subscribe_form" style="margin-bottom: 5px" action="{{ route('subscribe.store') }}">
@csrf
<input type="email" name="email" placeholder="Enter Your Email" id="subemail">
<button type="submit" class="btn btn-dark sub" id="subscribe">Subscribe</button>
</form>
@csrf
订阅
若你们仍然得到500个错误,那个么你们可以像屏幕截图那个样,你们将得到错误的确切内容
您可以像这样更改ajax和表单代码吗 Ajax代码
Route::post('subscribe/store', 'SubscribeController@store')
->name('subscribe.store');
$('body').on('submit','#subscribe_form',function(event){
event.preventDefault();
console.log('a');
$.ajax({
url : $(this).attr('action'),
data: new FormData(this),
type: 'POST',
contentType: false,
cache: false,
processData:false,
beforeSend: function () {
},
success: function () {
console.log('success');
}
});
});
<form id="subscribe_form" style="margin-bottom: 5px" action="{{ route('subscribe.store') }}">
@csrf
<input type="email" name="email" placeholder="Enter Your Email" id="subemail">
<button type="submit" class="btn btn-dark sub" id="subscribe">Subscribe</button>
</form>
表格代码
Route::post('subscribe/store', 'SubscribeController@store')
->name('subscribe.store');
$('body').on('submit','#subscribe_form',function(event){
event.preventDefault();
console.log('a');
$.ajax({
url : $(this).attr('action'),
data: new FormData(this),
type: 'POST',
contentType: false,
cache: false,
processData:false,
beforeSend: function () {
},
success: function () {
console.log('success');
}
});
});
<form id="subscribe_form" style="margin-bottom: 5px" action="{{ route('subscribe.store') }}">
@csrf
<input type="email" name="email" placeholder="Enter Your Email" id="subemail">
<button type="submit" class="btn btn-dark sub" id="subscribe">Subscribe</button>
</form>
@csrf
订阅
若你们仍然得到500个错误,那个么你们可以像屏幕截图那个样,你们将得到错误的确切内容
请添加一个错误截图。最好在此处添加错误消息。您可以单击该链接吗?它会显示错误您到底遇到了什么错误?我看到了。这还不够。单击
网络
选项卡并查看错误消息。请添加您收到的错误的屏幕截图。最好在此处添加错误消息。您可以单击该链接吗?它将显示错误您收到的错误具体是什么?我看到了。这还不够。单击网络
选项卡并查看错误消息。您是否单击了500存储方法并能告诉我错误是什么?否,单击预览旁边的响应。你会在那里看到实际的错误。检查预览哦,我发现了问题。我写了不正确的控制器名称。非常感谢。好的,下次使用ajax时请小心,您的错误将显示在网络中。您是否单击了“500存储”方法,并能告诉我错误是什么?否,单击预览旁边的“响应”。你会在那里看到实际的错误。检查预览哦,我发现了问题。我写了不正确的控制器名称。非常感谢。好的,下次使用ajax时要小心,错误将显示在网络中。