Php Laravel 5“内部服务器错误”(500)与Ajax表单提交
在我的Php Laravel 5“内部服务器错误”(500)与Ajax表单提交,php,ajax,laravel-5,http-status-code-500,Php,Ajax,Laravel 5,Http Status Code 500,在我的laravel5项目中,我的ajax表单提交有问题。我的浏览器控制台中出现以下错误: Failed to load resource: the server responded with a status of 500 (Internal Server Error) 我尝试了很多在其他帖子和网站上找到的建议,但都没有成功。以下是我的Ajax: $.ajax({'url': './subscribe' ,'data' : { "_token":
laravel5
项目中,我的ajax
表单提交有问题。我的浏览器控制台中出现以下错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
我尝试了很多在其他帖子和网站上找到的建议,但都没有成功。以下是我的Ajax:
$.ajax({'url': './subscribe'
,'data' : {
"_token": $('meta[name="csrf-token"]').attr('content')
,"email" : $('#subscribe-email-input').val()
}
,'method': 'POST'
,'success': function(data){
console.log( data );
}
,'error': function(data){
console.log( 'oops', data );
}
});
我在我的routes/web.php
文件中定义了以下路由:
Route::post('subscribe', ['uses' => 'SubscribeController@validate', 'as' => 'subscribe.route']);
控制器非常简单:
class SubscribeController extends Controller
{
/*
* Get email address from DB if exists
*
* @param string email, email address to search and return data from DB
*
* @returns $email
*/
public function validate ( Request $request )
{
$email = addslashes($request->email) ;
if ( $email )
{
$email = \App\Subscribe::where('email', '=', $email)->get();
if ( $email->isEmpty() )
{
self::executeInsertQuery ( $email );
return json_encode( array('body' => 'You have subscribed!') );
} else {
return json_encode( array('body' => 'You are already subscribed!') );
}
}
}
}
我会在self::executeInsertQuery(…)
中发布代码,但考虑到subscribe
路径甚至似乎没有到达validate
方法,我认为这不相关
如果您对如何解决此问题有任何建议,我们将不胜感激!!提前谢谢你 - 首先,在不使用ajax的情况下发送表单,并验证表单是否记录了发送的数据,然后在其ajax函数中,将参数“method”:“POST”更改为“type”:“POST”,然后重试。注意:jQuery3.0之前的版本需要用类型指示,否则默认情况下它将发送GET
- 其次,将名称方法“validate”更改为“validateSuscriber”,因为控制器类已经有了一个“validate”方法,并且由于有两个相等的方法而产生了冲突
- 首先,在不使用ajax的情况下发送表单,并验证表单是否记录了发送的数据,然后在其ajax函数中,将参数“method”:“POST”更改为“type”:“POST”,然后重试。注意:jQuery3.0之前的版本需要用类型指示,否则默认情况下它将发送GET
- 其次,将名称方法“validate”更改为“validateSuscriber”,因为控制器类已经有了一个“validate”方法,并且由于有两个相等的方法而产生了冲突
return response()->json([
'body' => 'You have subscribed!'
]);
试试看
问题在于
验证
功能是Laravel控制器中的预定义方法,因此您可以覆盖它,从而导致问题。尝试将其更改为其他内容。问题在于验证
功能是Laravel控制器中的预定义方法,因此您可以覆盖它,从而导致问题。尝试将其更改为其他内容。@KirkBeard此特定错误似乎不会导致新的日志条目被删除inserted@KirkBeard这个特定的错误似乎不会导致插入新的日志条目Hanks,我尝试在没有Ajax的情况下导航到路由(使用浏览器中的路由)它将我路由到Laravel方法NotAllowedHttpException
页面。我还尝试过将方法
切换到类型
,但它没有修复错误。如果您在没有ajax的情况下发送,请不要忘记在表单中添加csrf_字段()。还有:好的,我使用表单操作和方法手动发布,并删除了Ajax调用。现在,我将此消息重定向到Laravel错误页面,并显示以下消息:App\Http\Controllers\SubscribeController::validate(light\Http\Request$Request)的声明应与App\Http\Controllers\Controllers::validate兼容(illumb\Http\Request$Request,array$rules,array$messages=array,array$customAttributes=array)
将名称方法“validate”更改为“validateuscriber”,因为根据错误,控制器方法已经有了一个方法,这会产生冲突谢谢,我尝试在没有Ajax的情况下导航到路由(使用浏览器中的路由)并将我路由到Laravel方法NotAllowedHttpException
页面。我还尝试将方法切换到类型
,但没有修复错误。别忘了添加csrf\u字段()在表单中,如果您在不使用ajax的情况下发送它。另外:好的,我使用表单操作和方法手动发布,并删除了ajax调用。现在,我将此重定向到Laravel错误页面,并显示以下消息:App\Http\Controllers\SubscribeController::validate(illumb\Http\Request$Request)应与App\Http\Controllers\Controller::validate兼容(照亮\Http\Request$Request,数组$rules,数组$messages=array,数组$customAttributes=array)
将名称方法“validate”更改为“validatesUserScriber”,因为根据错误,控制器方法已经有了一个方法,这使得Conflict我更新了它以使用它,并且我仍然在函数中得到相同的errorTry注释all other行,只要保持return json
test and view logI更新它以使用它,我仍然在函数中得到相同的errorTry注释all other行函数,只需保留return json
test and view log这解决了我的问题。显然,根据错误,validate是基类控制器中的保留方法。我将方法名称从validate更改为verifySubscribe,一切正常!!这解决了我的问题。显然,根据错误,validate是保留方法方法。我将方法名从validate更改为verifySubscribe,一切正常!!