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

为什么我在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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分