2016-09-27 144 views
0

我一直在試圖讓我的POST工作,但沒有什麼是真正的工作。我有我的更新和刪除工作,但我仍然收到500錯誤,當我試圖發佈的東西。Laravel 5.3,Ajax後500錯誤

我的文件上已經有了我的CSRF-TOKEN。

//create new task/update existing task 
$("#btn-save").click(function (e) { 
    e.preventDefault(); 

    var formData = { 
     task: $('#task').val(), 
     description: $('#description').val(), 
    } 

    //used to determine the http verb to use [add=POST], [update=PUT] 
    var state = $('#btn-save').val(); 

    var type = "POST"; //for creating new resource 
    var task_id = $('#task_id').val();; 
    var my_url = url; 

    if (state == "update"){ 
     type = "PUT"; //for updating existing resource 
     my_url += '/' + task_id; 
    } 

    console.log(formData); 

    $.ajax({ 
     type: type, 
     url: my_url, 
     data: formData, 
     dataType: 'json', 
     success: function (data){ 
      console.log(data); 

      var task = '<tr id="task' + data.id + '"><td>' + data.task + '</td><td>' + data.description + '</td><td>' + data.created_at + '</td><td>' + data.done + '</td>'; 
      task += '<td><button class="btn btn-warning btn-xs btn-detail open-modal" value="' + data.id + '">Bewerk</button>'; 
      task += '<button class="btn btn-danger btn-xs btn-delete delete-task" value="' + data.id + '">Verwijder</button></td></tr>'; 

      if (state == "add"){ //if user added a new record 
       $('#tasks-list').append(task); 
      }else{ //if user updated an existing record 

       $("#task" + task_id).replaceWith(task); 
      } 

      $('#frmTasks').trigger("reset"); 

      $('#myModal').modal('hide') 
     }, 
     error: function (data) { 
      console.log('Error:', data); 
     } 
    }); 
}); 
}); 
+2

500錯誤會來自你的PHP代碼,而不是在這裏列出的JavaScript代碼。檢查你的錯誤日誌,找出它爲什麼投擲500. – aynber

+0

錯誤500似乎是權限錯誤。嘗試檢查文件的權限。 –

+0

你可以發佈你的Laravel代碼,包括你定義這些URI的路線嗎? – tptcat

回答

1

500意味着它的內部服務器錯誤,所以你的php中的東西是錯誤的。

打開您的存儲/日誌/ laravel.log並轉到您的最後一個錯誤。這給你一個線索是什麼問題。

(如果你在.ENV有APP_DEBUG=true,那麼你也應該能夠檢查在瀏覽器中的錯誤響應。)

+0

真棒! 。非常感謝! –

+1

它是什麼,接受的答案是? – hogan

+0

它說我的表中有一行沒有默認值。奇怪的是,類似的東西弄亂了所有的東西。 –