lundi 6 février 2017

Laravel and AJAX 405 (Method Not Allowed) work on one page only

I have Note function this note function allow the user to create not from any route in the web app.

JQuery Code

//CREATE NOTE BUTTON
$('#createNoteButton').click(function () {
    var title = $('#getNoteTitle').val();
    var body  = $('#getNoteBody').val();
    var users = $('#getNoteUsers').val();
    var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
    var data  = {
        _token:     CSRF_TOKEN,
        title:      title,
        body:       body,
        users:      users
    }

    $.ajax({
        type:'POST',
        url:'note-create',
        data: data,
        success:function(data){
            if(data == 1)
                location.reload();
        }
    });
});

NotesController.php

public function create(Request $request){
        $logged_user_id     = Auth::user() -> id;
        $logged_user_name   = Auth::user() -> name;
        $note = new Note;
        $note -> title      = $request -> title;
        $note -> body       = $request -> body;
        $note -> owner_name = $logged_user_name;
        $note -> owner_id   = $logged_user_id;
        $note -> save();
        $users = $request -> users;
        if($users != '')
            array_push($users, $logged_user_id);
        else
            $users = [$logged_user_id];

        $note -> users() -> attach($users);

        return '1';
    }

Route Code

Route::post('note-create',         'NotesController@create');

Problem

Everything works perfectly but only in the home page route('/') and any other route I get this error message 405 (Method Not Allowed).

What I have tried

I tried to check the url in the route and in the ajax request and both is right I think



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

Aucun commentaire:

Enregistrer un commentaire