I'm trying to make an ajax post in laravel 4, but I'm getting a 500 (Internal Server Error). I've looked over the internet and I can't see what I'm doing wrong.
My ajax post (executed from 'http://flashwords.dev/app/public/words/level/1'):
$.ajax({
url: 'http://flashwords.dev/app/public/words/post_set',
type:'POST',
dataType:'JSON',
data:{
"words_correct": words_correct,
"words_false" : words_false
},
success:function(){
console.log("post success");
}
});
My route:
Route::post('words/post_set', array('as'=>'post_set', 'uses'=>'UserController@post_set'));
My function in the UserController:
public function post_set(){
$correct = Input::get('words_correct');
$false = Input::get('words_false');
var_dump($correct);
}
Probably I'm looking over something dumb..
Thanks in advance!
You should change your url
from:
url: 'http://flashwords.dev/app/public/words/post_set',
To following:
url: 'words/post_set',
Hope your controllers are stored in the app/controllers
folder. You may also use this:
url: {{ route('post_set') }}
If this is in a .php
file and using blade, if not blade then remove the {{}}
and use echo
instead.
Try using the Route's name to get it's full path and set it to the JSON request:
url:'<?=URL::route('post_set')?>',
type:'POST',
dataType:'JSON'
...stuff
I use such style in my own project, and it works just fine.
So, I found out the problem.
My code was fine, my url was fine, BUT because I'm trying to post data to my server laravel expect a csrf token sent with the post request.
How? --> http://words.weareloring.com/development/laravel/laravel-4-csrf-tokens-when-using-jquerys-ajax/
It was a security issue.