2016-12-01 8 views
1

我新的angularjs做一個CRUD我使用的功能,這將是使用兩個更新和保存我的功能碼

$scope.save = function (modalstate, id) { 
     var url = $scope.url; 
     var method = "POST"; 
     console.log(modalstate); 
     //append id to the URL if the form is in edit mode 
     if (modalstate === 'edit') { 
      url += "/" + id; 
      method = 'PATCH'; 
     } 
     var data = {}; 
     data = $scope.formdata; 

     if (!$("#myform").isValid()) { 
      return false; 
     } else { 
      console.log('request'); 
      $http({ 
       method: method, 
       url: url, 
       data: data, 
       headers: {'Content-Type': undefined}, 
       transformRequest: function (data) { 
        console.log(angular.toJson(data)); 
        var formData = new FormData(); 
        formData.append("data", [angular.toJson(data)]); 

        if (typeof (data.logo) != "undefined") 
        { 
         formData.append("file", data.logo); 
        } 
        console.log(formData); 
        return formData; 
       }, 
      }).success(function (response) { 
       console.log(response); 
       // location.reload(); 
      }).error(function (response) { 
       console.log(response); 
       alert('This is embarassing. An error has occured. Please check the log for details'); 
      }); 
     } 

以上功能是完美的,當我存儲記錄。但在編輯的更新記錄我使用PATCH請求,但現在通過使用PATCH請求我收到了服務器端的空數組問題。請幫助我解決我的問題。

+0

你能告訴我們你的'控制器\ action'並提供通路登錄 – xAoc

+0

我只是用與資源路徑'路線::資源的代碼(」人」, '把PeopleController');' –

回答

1

Laravel利用method spoofing爲更好地支持RESTful界面,例如您正在嘗試使用Route::resource構建的界面。這意味着,而不是使用方法PATCH,您使用POST方法,並與您的請求數據一起傳遞與價值PATCH一個_method關鍵。

所以,你的代碼可能是這樣的:

$scope.save = function (modalstate, id) { 
     var url = $scope.url, 
      data = $scope.formdata; 

     //append id to the URL if the form is in edit mode 
     if (modalstate === 'edit') { 
      url += "/" + id; 
      data = angular.merge(data, {_method: 'PATCH'}); 
     } 

     if (!$("#myform").isValid()) { 
      return false; 
     } else { 
      console.log('request'); 
      $http({ 
       method: "POST", 
       url: url, 
       data: data, 
       headers: {'Content-Type': undefined}, 
       transformRequest: function (data) { 
        console.log(angular.toJson(data)); 
        var formData = new FormData(); 
        formData.append("data", [angular.toJson(data)]); 

        if (typeof (data.logo) != "undefined") 
        { 
         formData.append("file", data.logo); 
        } 
        console.log(formData); 
        return formData; 
       }, 
      }).success(function (response) { 
       console.log(response); 
       // location.reload(); 
      }).error(function (response) { 
       console.log(response); 
       alert('This is embarassing. An error has occured. Please check the log for details'); 
      }); 
     } 
相關問題