tmybr11's avatar

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!

0 likes
13 replies
jekinney's avatar

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.

tykus's avatar

What does the error say - check in your browsers dev tools?

nikocraft's avatar
Input::get("id")

thats the problem, you are not sending that in your ajax post

tmybr11's avatar

Well, that's what shows up when I click the red request:

jquery-3.1.1.js:9536 POST http://localhost:8000/updater 500 (Internal Server Error)
send @ jquery-3.1.1.js:9536
ajax @ jquery-3.1.1.js:9143
(anonymous) @ updater.js:9
mightThrow @ jquery-3.1.1.js:3570
process @ jquery-3.1.1.js:3638

And maxnb, you are right, I forgot sending the id. But I fixed that and it still returns me 500.

nikocraft's avatar

You are probably failing here

$nation = Nation::find(Input::get("id"))

It probably finds nothing and in next line you are doing this:

        $nation->population = $nation->population * mt_rand(1,3);
        
        $nation->save();

which will fail since nothing was found.

Since you do not understand how to use your browser to get a preview of the page that laravel returns where it gives you complete description of the error and which was described by other users above, you will need to start commenting out code until you get it to work, and then step by step uncomment it untill it starts to fail again. Then you will know what is failing.

tmybr11's avatar

I think the method makeUpdate() doesn't event run. I'm pretty sure Nation::find(Input::get("id")) will find something, so that can't be causing the error. And I commented all the database-related lines. Nothing happened.

In fact, nothing happens even when I var_dump or dd the $nation variable.

cent040's avatar

check your route it must be POST

and your ajax URL

 url    : {{(url('updater')}},

OR

url : "<?php echo url('updater'); ?>",

Regards Arfan

MaverickChan's avatar

@tmybr11 your csrftoken name does not match

try

var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
    
    $.ajaxSetup({
        headers: { 'X-CSRF-Token' : $('meta[name=csrf-token]').attr('content') }
    });

Please or to participate in this conversation.