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 !

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

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型