If you're testing in browser, inspect click network tab and do what ever action required. You'll see a red request. Click it and you'll see a detailed error message.
Jan 8, 2017
13
Level 1
Ajax returning 500 internal server error
Hello guys, I'm new to Ajax and I am having a lot of trouble figuring out why I'm getting 500 status code everytime. I'm using POST method, and the route is set to POST as well. I'm correctly passing the CSRF_TOKEN in the data, so I really don't get what is going on. Here is the code...
The jQuery:
$(document).ready(function() {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$.ajaxSetup({
headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
$.ajax({
type :"post",
url :"/updater",
dataType:"json",
data :{_token: CSRF_TOKEN,
pop: $("#pop-index").text(),
cash: $("#cash-index").text()},
timeout: 1000
})
});
The controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Response;
use App\Nation;
class GameInitialController extends Controller
{
/**
*
* Updates indexes
*
*/
public function makeUpdate()
{
$pop = Input::get("pop");
$cash = Input::get("cash");
$nation = Nation::find(Input::get("id"));
$nation->population = $nation->population * mt_rand(1,3);
$nation->save();
return Response::json(array("nation" => $nation));
}
}
The web.php file:
Route::post("/updater", "UpdateController@makeUpdate");
What could be wrong here? Thanks in advance!
Please or to participate in this conversation.