创建post方法并使用ajax和laravel传递id

时间:2018-06-30 11:48:02

标签: ajax laravel laravel-5.6 ajaxform

我正在尝试将ajax添加到我的方法laravel中。此方法在讨论页面下创建回复。它在Ajax之前运行,现在我想添加Ajax以使回复非常快!我之所以加入堆栈,是因为我尝试了很多方法来传递$ id(discussion_id),但没有人能做到!

我正在使用laravel 5.6

我的路线是:

Route::post('/discussion/reply/{id}', [
    'uses' => 'DiscussionsController@reply',
    'as' => 'discussion.reply'
]);

我的功能控制器是:

   public function reply($id)
{
    $this->validate(request(), [
        'reply' =>'required',
    ]);

    $d = Discussion::find($id);

    $reply = Reply::create([
        'user_id' => Auth::id(),
        'discussion_id' => $id,
        'content' => request()->reply
    ]);


    Session::flash('success', 'Votre réponse a été enregistrer avec success');

    //return redirect()->back(); // this is what i was doing doing before the ajax



    return response()->json(['success'=>'réponse ajouter']);
}

我的JavaScript Ajax:

jQuery.ajax({
      url: "/discussion/reply/",
      method: 'post',
      data: {
        reply: jQuery('#reply').val(),
      },
      dataType: 'json',
      success: function(data){
              jQuery.each(data.errors, function(key, value){
                jQuery('.alert-danger').show();
                jQuery('.alert-danger').append('<p>'+value+'</p>');
            });
        }
    });

最后是表单刀片html:

<form action="{{ route('discussion.reply', ['id' => $d->id]) }}" method="POST">
                        {{ csrf_field() }}

                        <div class="form-group">
                            <label for="reply">Laisser une réponse...</label>
                            <textarea name="reply" id="reply" cols="30" rows="10" class="form-control"></textarea>
                        </div>

                        <div class="form-group">
                            <button class="btn pull-right" id="submit">Répondre</button>
                        </div>
                    </form>

1 个答案:

答案 0 :(得分:0)

您可以使用此代码.test请:

Ajax:

<script>
_token="{{csrf_token()}}";

$("form").submit(function(e){
    e.preventDefault();
    $.post("{{route('discussion.reply',['id' => $d->id])}}",
        {

            reply: $('#reply').val(),
            _token:_token
        },
        function(data, status){
            if(status=="success")
            {
                alert('success')
            }
            else
            {
                alert('show error')
            }

        });
});
</script>

DiscussionsController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class DiscussionsController extends Controller
{
    public function reply(Request $request,$id)
    {
        $validator = Validator::make($request->all(), [
            'reply' => 'required',
        ]);

        if ($validator->fails())
        {
            return response()->json(['errors'=>$validator->errors()->all()]);
        }


        $d = Discussion::find($id);

        $reply = Reply::create([
            'user_id' => Auth::id(),
            'discussion_id' => $id,
            'content' => request()->reply
        ]);


        return response()->json(['success'=>'réponse ajouter'],200);
    }
}