【发布时间】:2015-11-25 18:24:07
【问题描述】:
所以,我在 Laravel 5 中通过 ajax 发送发布请求时遇到了问题。 我在一个视图中有 2 个表单,其中一个正在进行 ajax 调用。
首先,路线:
Route::post('partidoBoostFree', 'PartidoController@boostFree');
Route::resource('partido', 'PartidoController');
这是在视图中进行调用的表单:
<div class="col-md-4 boostPanel">
<div class="panel-body">
<div class="input-group">
{!! Form::open([ 'url' => 'partidoBoostFree', 'id' => 'guardaBoostFree', 'method' => 'POST' ]) !!}
<input type="hidden" name="_token" value="{{{ Session::getToken() }}}">
{!! Form::hidden('id_boost_free', $partidoBoost->id, ['id' => 'id_boost_free']) !!}
{!! Form::text('juegatela', null, ['class' => 'form-control', 'id' => 'juegatela', 'placeholder' => 'Puntos a jugar']) !!}
</div>
{!! Form::submit('Juégatela', ['class' => 'btn btn-primary form-control', 'id'=>'juegatelaSbmt', 'disabled' => 'disabled']) !!}
{!! form::close() !!}
</div>
</div>
这是 JS:
$('#juegatelaSbmt').click(function(e){
e.preventDefault();
e.stopPropagation();
$.ajax({
url: '/partidoBoostFree',
method: 'POST',
data: {
'idPartido' : $('#id_boost_free').val(),
'puntos' : $('#juegatela').val(),
'_token': $('input[name=_token]').val()
},
success: function(data){
$('.boostPanel .panel-body').html(data);
}
})
});
最后是控制器:
public function boostFree(){
if ( Request::ajax() ){
$data = Input::all();
if( $data[puntos] > 0 && $data[puntos] < 4 ){
if( $this->partidoRepo->getPartido_Usuario( $data[idPartido], Auth::user() ) ){
$apuesta = $this->apuestaRepo->postApuestaFree( $data[idPartido], Auth::user(), $data[puntos] );
if($apuesta){
return "<div class='alert alert-info' role='alert'>
<p>Te la jugaste. Suerte!!!</p>
</div>";
}else{
flash()->error('Hubo un problema, intentalo más tarde');
}
}else{
//Regresa mensaje de que haga la quiniela por lo menos para el partido
}
}else{
//Regresa un mensaje de seleccionar el min o máximo de puntos a jugar.
}
}
}
有趣的是,当我在检查器中进行 ajax 操作时,我得到了这个:
但是,如果我评论 boostFree() 函数,我会得到:
问题是,无论是 200 还是 500,我从 Laravel 得到的都是 RouteCollection.php 第 207 行中的 MethodNotAllowedHttpException 错误
我几乎尝试了所有方法,将标记放在标题中,将发布操作更改为获取(我知道这不会起作用),将 .ajax 函数中的“类型”更改为“方法”。
希望有人可以帮助我,我真的很感激。 非常感谢。
【问题讨论】:
-
您的路线似乎配置为发布到
partidoBoostFree,而您的表单正在发布到/partidoBoostFree。