javascript - Why am I getting this Internal Server Error in the Laravel Framework?


I have come across a situation that doesn't make much sense to me. Just as some background information, I'm using the Laravel framework. The page in question calls a query when the page is requested using Laravel's '->with('var', $array)' syntax. This query (which I will post later) works perfectly fine on page load, and successfully inserts dummy data I fed it.

I call this same query via an Ajax $.post using jQuery, on click of a button. However, when I do this $.post and call this query, I get an Internal Server Error every time. Everything is exactly the same, information passed included; the only difference seems to be whether or not it is called on page load or via the $.post.

Here is the error:

enter image description here

Below is the code that performs the query on page load:

routes.php sends the HTTP get request to a file calledAppController.php

routes.php enter image description here AppController.php enter image description here

The page is then made with the following array acquired fromDeviceCheckoutController.php

enter image description here

Which then goes toDeviceCheckout.php

enter image description here

I am able to echo $test on the page, and it returns the ID of a new row every time the page is reloaded (which obviously mean the 'insertGetId' query worked). However, I hooked this query up to the page load just to test. What I really want to happen is on click of a button. Here is the code for that:

$("#checkoutFormbox").on('click', '#checkoutButton', function() {

        var checkoutInformation = Object();
        var accessories = [];
        var counter = 0;
        var deviceName = checkoutDeviceTable.cell(0, 0).data();

        $(".accessoryCheckbox").each(function() {
            //add accessory ID's to this list of only accessories selected to be checked out
            if($(this).val() == "1")
                accessories[counter] = $(this).data('id') + " ";

        checkoutInformation['deviceID'] = $(".removeButton").val(); //deviceID was previously stored in the remove button's value when the add button was clicked
        checkoutInformation['outBy'] = '';
        checkoutInformation['outNotes'] = $("#checkOutDeviceNotes").val();
        checkoutInformation['idOfAccessories'] = 2;
        checkoutInformation['dueDate'] = $("#dueDate").val();

            checkoutInformation['renterID'] = 0;
            checkoutInformation['emplid'] = 1778884;
            $.post("", {type: "checkoutDeviceForStudent", checkoutInformation: checkoutInformation}, function(returnedData) {

Which is also then routed toAppController.php, specifically to the 'checkoutDeviceForStudent' part of the switch statement:

enter image description here

And then back to that query that is shown previously inDeviceCheckout.php

Finally, here is my DB structure for reference:

enter image description here

Any explanation as for why this would be happening? Also, any Laravel or other general best practice tips would be greatly appreciated as I'm inexperienced in usage of this framework and programming overall.

Sorry for such a long post, I hope there is enough information to diagnose this problem. Let me know if I need to include anything else.

Edit: Included picture of error at the top of the page.




Everything is exactly the same, information passed included

No, it isn't. If it was exactly the same you wouldn't be getting the error you're getting.

These sorts of issues are too difficult to solve by taking guesses at what the problem might be. You need to

  1. Setup your system so Laravel's logging errors to the laravel.log file

  2. Setup you PHP system so errors Laravel can't handled are logged to your webserver's error log (and/or PHP's error log)

  3. Put Laravel in debug mode so errors are output the the screen, and the view the output of your ajax request via Firebug or Chrome

Once you have the actual PHP error it's usually pretty easy to see what's different about the request you think is the same, and address the issue.




I found a resolution to my problem after some advice from a friend; much easier than I anticipated and much easier than any solution that has been offered to me here or other places.

Essentially, what I needed to do was place a try, catch clause in my model function, and then if an exception is encountered I store that in a variable,return it, and useconsole.log() to view the exception. Here is an example to emulate my point:

public function getUserFullname($userID)
        $myResult = DB::connection('myDatabase')->table('TheCoolestTable')->select('fullName')->where('userID', '=', $userID)->get();

        return $myResult;  
    catch(Exception $e)
        $errorMessage = 'Caught exception: ' . $e->getMessage();

        return $errorMessage;

And then on the View (or wherever your model function returns to), simplyconsole.log() the output of your POST. This will display the results of the successful query, or the results of the Exception if it encountered one as opposed to an unhelpful Internal Server Error 500 message.

People are also looking for solutions to the problem: jquery - Indefinate Pagination with PHP


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.