dongtiao0279
2014-08-25 15:11
浏览 107
已采纳

为什么我在Laravel框架中出现此内部服务器错误?

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 called AppController.php

routes.php enter image description hereAppController.php enter image description here

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

enter image description here

Which then goes to DeviceCheckout.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') + " ";
            }
            counter++;
        });

        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();

        if($("#studentIdButton").hasClass('active'))
        {
            checkoutInformation['renterID'] = 0;
            checkoutInformation['emplid'] = 1778884;
            console.log(checkoutInformation);
            $.post("http://xxx.xxx.xxx.xxx/testing/public/apps/devicecheckout-checkoutdevices", {type: "checkoutDeviceForStudent", checkoutInformation: checkoutInformation}, function(returnedData) {
                alert(returnedData);
            });
        }
});

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

enter image description here

And then back to that query that is shown previously in DeviceCheckout.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.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dougupang0901 2014-09-15 21:48
    已采纳

    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 use console.log() to view the exception. Here is an example to emulate my point:

    public function getUserFullname($userID)
    {
        try
        {
            $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), simply console.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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douwen1006 2014-08-25 17:15

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题