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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)