weixin_33725515 2016-02-02 05:16 采纳率: 0%
浏览 21

Laravel AJAX表格405错误

I'm receiving this error when attempting to submit via AJAX. When not using AJAX, the form submits just fine to it's specified url. I've read this error can happen because the form is trying to submit in the browser along with the AJAX request. I've tried using onsubmit="event.preventDefault()".

ROUTE:

Route::post('/post/{id}', [
    'uses' => '\App\Http\Controllers\PostController@postMessage',
    'as' => 'post.message',
    'middleware' => ['auth'],
]);

FORM:

<form id="post" role="form" action="{{ route('post.message', ['id' => $user->id]) }}" onsubmit="event.preventDefault()">
    <div class="feed-post form-group{{ $errors->has('post') ? ' has-error' : ''}}">
        <textarea class="form-control feed-post-input" rows="2" id="postbody" name="post" placeholder="What's up?"></textarea>
        <div class="btn-bar">
            <!-- <button type="button" class="btn btn-default btn-img btn-post" title="Attach an image"><span class="glyphicon glyphicon-picture"></span></button> -->
            <!-- <input type="file" id="img-upload" style="display:none"/> -->
            <button class="btn btn-default btn-post" title="Post your message"><span class="glyphicon glyphicon-ok"></span></button>
        </div>
        @if ($errors->has('post'))
            <span class="help-block">{{ $errors->first('post') }}</span>
        @endif
    </div>
    <input type="hidden" name="_token" value="{{ CSRF_token() }}">
</form>

CONTROLLER:

public function postMessage(Request $request, $id)
{

        $this->validate($request, [
            'post' => 'required|max:1000',
        ]);
  if(Request::ajax()){
         Auth::user()->posts()->create([
             'body' => $request->input('post'),
             'profile_id' => $id
         ]);
  }
}

JS:

$('#post').submit(function(){
    var body = $('#postbody').val();
    var profileId = $('#user_id').text();
    var postRoute = '/post/'+profileId;

    var dataString = "body="+body+"&profile_Id="+profileId;

    $.ajax({
        type: "POST",
        url: postRoute,
        data: dataString,
        success: function(data){
            console.log(data);
        }
    });
});
  • 写回答

1条回答 默认 最新

  • weixin_33716154 2016-02-02 07:06
    关注

    try this, remove

    onsubmit="....." attribute from form
    

    and update your submit method like this

    $('#post').submit(function(event){
        var body = $('#postbody').val();
        var profileId = $('#user_id').text();
        var postRoute = '/post/'+profileId;
    
        var dataString = "body="+body+"&profile_Id="+profileId;
    
        $.ajax({
            type: "POST",
            url: postRoute,
            data: dataString,
            success: function(data){
                console.log(data);
            }
        });
    
       //this will prevent your default form submit 
       event.preventDefault();
    });
    

    I hope this will work for you

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错