douwen2158 2014-04-17 08:39
浏览 55
已采纳

当我尝试使用数据进行数据库请求时,为什么Laravel上的AJAX GET / POST会出现“内部服务器错误”?

I have a special issue that i'm not able to fix...

I drag & drop some event in a calendar. When I drop them, I send an Ajax Post Request to my controller, which is able to insert in database some stuff with the event name that I sent via ajax.

first, there is my code in my JS file :

$("#someIDstuffHere").droppable({
    appendTo: "body",
    tolerance: "pointer",
    accept: ".external-event",
    drop: function(event, ui) {
        $(this).append($(ui.draggable));
        $.ajax({
            type: 'POST',
            url: 'events/add-cron-event',
            data: {
                "eventName": $(ui.draggable).text() // get the span text
            },
            success: function(data) {
                console.log("success");
            },
            error: function() {

            }
        });
    }
});

My EventController.php

public function postAddCronEvent(){ // Laravel use RESTful, so function can have post/get + url like add-cron-event
    $data['input'] = Input::get('eventName'); // get the data from POST
    $cronState = CronState::where('name', '=', $data['input'])->take(1)->get(); // Laravel Eloquent ORM make me able to do that kind of request... 
    foreach ($cronState as $state) {
        var_dump($state);
    }

}

And the var_dump result (beautified) when I use that via GET (not POST).

object(
CronState
)#234 (
18
) {
[\"table\":protected]=> string(
9
) \"cronState\"
[\"connection\":protected]=> NULL
[\"primaryKey\":protected]=> string(
2
) \"id\"
[\"perPage\":protected]=> int(
15
)
[\"incrementing\"]=> bool(
true
)
[\"timestamps\"]=> bool(
true
)
[\"attributes\":protected]=> array(
4
) {
[\"id\"]=> string(
2
) \"14\"
[\"name\"]=> string(
5
) \"test2\"
[\"created_at\"]=> string(
19
) \"2014-04-16 12:35:35\"
[\"updated_at\"]=> string(
19
) \"2014-04-16 12:35:35\" }
[\"original\":protected]=> array(
4
) {
[\"id\"]=> string(
2
) \"14\"
[\"name\"]=> string(
5
) \"test2\"
[\"created_at\"]=> string(
19
) \"2014-04-16 12:35:35\"
[\"updated_at\"]=> string(
19
) \"2014-04-16 12:35:35\" }
[\"relations\":protected]=> array(
0
) { }
[\"hidden\":protected]=> array(
0
) { }
[\"visible\":protected]=> array(
0
) { }
[\"appends\":protected]=> array(
0
) { }
[\"fillable\":protected]=> array(
0
) { }
[\"guarded\":protected]=> array(
1
) {
[0]=> string(
1
) \"*\" }
[\"touches\":protected]=> array(
0
) { }
[\"with\":protected]=> array(
0
) { }
[\"exists\"]=> bool(
true
)
[\"softDelete\":protected]=> bool(
false
) }

When I do that, it works.

    success: function(data) {
        console.log("success");
    }

Show me "success" in the console.log...

But, NOW, when I do :

foreach ($cronState as $state) {
    $cronEvent = CronEvent::find($state->id);
    $cronEvent->h = 8;
    $cronEvent->save();
}

My Chromium console said "Internal Server Error 500" (GET and POST tested)...

I think the mistake comes from the var_dumped data... It looks like very complicated...

Thanks for help !

EDIT to anwser to comments :

127.0.0.1 - - [17/Apr/2014:10:52:48 +0200] "POST /stage/public/events/add-cron-event HTTP/1.1" 500 1536 "localhost/stage/public/events" "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/33.0.1750.152 Chrome/33.0.1750.152 Safari/537.36"

That is the only error I can see in my error.log or access.log (this error is from access.log...)

EDIT AGAIN :

It's seem to be a problem with $cronEvent = CronEvent::find($state->id); ...

$state->id don't works...

But if I put "21" or another id, it works !

I tried to convert to Int with intval() but it doesn't works again...

ANSWERED

I auto answered with some investigations...

It's seem to be the Eloquent request...

I did a mistake. I tried to update the primary key, but primary key with "13" or "14" doesn't exists in my DB.

But I tried to use Eloquent after correcting this mistake, but it doesn't works again...

So,

I change that by :

DB::table('cronEvent')
  ->where('state_id', $state->id)
  ->update(array('h' => 10));

I used the Query Builder Instead of the Eloquent, and it's works !

  • 写回答

1条回答 默认 最新

  • doucepei5298 2014-04-18 06:10
    关注

    ANSWERED

    I auto answered with some investigations...

    It's seem to be the Eloquent request...

    I did a mistake. I tried to update the primary key, but primary key with "13" or "14" doesn't exists in my DB.

    But I tried to use Eloquent after correcting this mistake, but it doesn't works again...

    So,

    I change that by :

    DB::table('cronEvent')
      ->where('state_id', $state->id)
      ->update(array('h' => 10));
    

    I used the Query Builder Instead of the Eloquent, and it's works !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?