Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

penguinfeet's avatar

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

I am trying to run an Ajax call using a basic user resource to store some input data. However, try as I might, I cannot seem to get beyond this 500 ISE. Here is the call (within a React.js function):

saveAndContinue: function(e) { e.preventDefault()

// Get values via this.refs

email = this.refs.email.getDOMNode().value
this.setState({email: email})
this.setState({submitted: !this.state.submitted});

request = $.ajax({ 
      url: "/user", 
      type: "post", success:function(data){
      }, 
      data: {'email': email} ,beforeSend: function(data){console.log(data);} 
});

}

My Controller (UserController.php):

use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Routing\Controller;

class UserController extends Controller {

/**
 * Store user info.
 *
 * 
 * @return Response
 */
public function store() {

    $v = Fan::validate(Input::all());

    $email = Input::get('email');

    if ( $v->passes() ) {  

        $user = new User;
        $user->email = Input::get('email');
        $user->save();

    }

    return $email;

}

}

I am using the model that comes pre-baked with Laravel 5.

The route to set up the resource is:

Route::resource('user', 'UserController');

This is extraordinarily frustrating. What could I possibly be doing wrong? I suspect this is something to do with Laravel 5 syntax (in my controller or something else) that is causing this error. Thank you in advance for your help!

0 likes
5 replies
JohnRivs's avatar

You gotta provide the CSRF token. The usual way when using AJAX is, create a meta tag in your template where you set the value to csrf_token(), then either pass it as another POST value ({_token: theCSRFToken}) or as a HTTP Header (I think it's X-CSRF-TOKEN).

If you a have more than 1 or 2 places where you need to this, I suggest doing this:

$.ajaxSetup({
   headers: {'X-CSRF-Token': $('meta[name=csrf_token]').attr('content')}
});
penguinfeet's avatar

So I set this up like this:

<meta name="csrf-token" content="<?= csrf_token() ?>">

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

request = $.ajax({ 
          url: "/user", 
          type: "post", success:function(data){
          }, 
          data: {'email': email} ,beforeSend: function(data){console.log(data);} 
    });

This is still not working. Same error.

mchumer's avatar

I've done!!

Try to change your controller...

Mine is like this.

public function pricetableItemCreate(Request $request)
{
    
    $pricetableitem = new PriceTableItem();
    $input = $request->all();

    $pricetableitem->price_table_id = $input['price_table_id'];
    $pricetableitem->item_id = $input['item_id'];
    $pricetableitem->rate = $input['rate'];
    $pricetableitem->price = $input['price'];
    
    $pricetableitem->save();

    return response()->json($pricetableitem, 201);
}   

I've used your ajax part.

1 like

Please or to participate in this conversation.