vendredi 10 février 2017

How to use ajax in laravel 5.3

I am new at Larvael.I am using laravel 5.3. I want to make a text field where it will autometicaly suggest some data and when i select a data it will add to an array. i want to send that arry to controller for further using. For this the

view file is as following:

<head>
    <script src="http://ift.tt/1dLCJcb"></script>
    <script src="http://ift.tt/1NKbGQl"></script>
    <script>
        $(document).ready(function() {
            var members = {!!  json_encode($member)  !!};
            console.log(members);
            var arr = [];
            $("#tags").autocomplete({
                source: members,
                select: function (event, ui) {
                    arr.push(ui);
                    console.log(arr);
                }
            });
            $("#submit").click(function(event){
                $.ajax({
                    type: "POST",
                    url: '/storeresearch',
                    data: {selectedMembers: arr},
                    success: function( msg ) {
                        console.log(msg);
                    }
                });
            });
        });
        </script>
</head>
<body>
<form id="hu" action="/storeresearch" method="POST">
    {!! csrf_field() !!}
    <label>Research Author</label>
    <input type="text" id="tags" name="researchsupervisor_1" value="">
    <input type="submit" name="submit" id="submit" class="btn btn-primary" value="Add">
</form>
</body>

My Controller file is as following:

public function store(Request $request){
        if($request->ajax())
        {
            $mem = $request->all();
            return response()->json($mem,200) ;
        }
        else{
            return "not found";
        } 

And web.php is as followings:

Route::post('/storeresearch','ResearchController@store');

But it seems that there is no ajax call happening. In the controller it always enters the else section. What is the problem can any one help?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kaSsZw
via IFTTT

Aucun commentaire:

Enregistrer un commentaire