10

I need to receive data from jQuery post request, think there is some error with routs or controller, here is my post request javascript code:

$.post('http://localhost:8000/ajax',
                {

                    task: "comment_insert",
                    userID: _userID,
                    comment: _comment,
                    name: _name,
                    userName: _userName
                }

                ).error(
                    function(data)
                    {
                      alert("Error: "+ data); 
                    }
                 )
                .success(
                    function( data )
                    {
                        comment_insert(jQuery.parseJSON( data ));
                      console.log("RESPOND TEXT:" + data);

                    }
                 );

     }

Also here is my routes for Laravel framework:

Route::post('ajax', 'AjaxController@index');

Controller:

class AjaxController extends Controller {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function index()
    {
             return view('ajax.ajax');

    }
}

my ajax.php script is into /resource/views/ajax/ajax.php Also if I put script into /public/ajax/ajax.php all works fine....I use Laravel 5... Please help

EDIT:

I found what is problem but don't know how to solve.

When I disable csrf protection from: kernel.php code work anyone know how to make code work with csrf protection enabled?

2 Answers 2

13

UPDATE: The problem is that the new CSRF-protection does not work with ajax-requests. Here is what you could do:

In your master template add a new meta tag with the current token like this

 <meta name="csrf-token" content="{{ Session::token() }}"> 

Then when sending your ajax call you add the token like this:

$.post('http://localhost:8000/ajax',
    {
        '_token': $('meta[name=csrf-token]').attr('content'),
        task: 'comment_insert',
        userID: _userID,
        comment: _comment,
        name: _name,
        userName: _userName
    })
    .error(
        ...
     )
    .success(
        ...
     );
}
Sign up to request clarification or add additional context in comments.

2 Comments

It not soleve problem.... Here is error: POST localhost:8000/ajax 500 (Internal Server Error)
Could you post the full error on pastbin and then a link to it here? You can find it in the logs (storage/logs/laravel...).
1

Is a simple code with javascript to send methods GET,POST,PUT,DELETE

declare header: <meta name="csrf-token" content="{{ Session::token() }}">

  function addCarrito(Urldir,paramt)
        {

        $(function(){
         $.post(Urldir,{ _token: $('meta[name=csrf-token]').attr('content'), _method : 'PUT', data :  }, function(response){

               if(response != '')
                {
                 console.log('good');
                }

            });
        });
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.