2017-03-01 126 views
1

我不是一個有經驗的開發人員,但我目前正在學徒,而且我的高級開發人員最近離開了我,並獨自一人前往!從GET更改爲POST

我從他那裏聽說,使用'GET'請求比'POST'更好,但是在讀完後我想切換到'POST',我唯一的問題是我有點不確定如何修改我的代碼來切換從'GET'結束。下面我已經在我們的系統中留下了一個共同的範圍。任何幫助將是偉大的

$scope.changenote=function(id,note){ 
note = encodeURIComponent(note); 
var inserthistory = './dbscripts/solicitors/changenote.php?id=' + id + '&user=' + AppStorage.getCurrentUser().username + '&note=' + note; 
console.log(inserthistory); 
        $http({method: 'GET', url: inserthistory}).success(function(data) { 

     var historyurl = './dbscripts/solicitors/gethistory.php?ref=' + $scope.bbref; 

      $http({ 
       method: 'GET', 
       url: historyurl 
      }).success(function(data) { 
       $scope.histnoteaddbtn = []; 
       $scope.history = data; 
      $state.go($state.current, {}, { 
        reload: true 

      }); 
    }); 
swal("Saved!", "Your note has been saved!", "success") 
}) 
} 
+0

那麼這取決於你在做什麼系統!我的意思是這是一個CMS或...?而且你不能總是把GET改成POST!有時你需要在一些功能中使用每一個!而關於安全性,POST和GET之間基本沒有什麼不同!在這兩種情況下,您必須使用準備好的查詢語句並在查詢中使用它之前檢查數據! – Soheyl

+0

@GeomanYabes大喊?哪裏? –

+1

** Soheyl **的評論..:D總是用'!' –

回答

2

很好,在第一種情況下,我重構了這些代碼。我不知道爲什麼所有這些字符串都保存到變量中,而它的代碼中再也沒有使用它。我真的不知道它的用途。我將您的請求從GET更改爲POST。您可能需要檢查您的API端點,並讓他們監聽POST請求。最終您的代碼如下所示:

注意:將請求數據解析爲請求的URL。要創建一個RESTful API,你應該看看這裏W3C HTTP Method Definition。所有的請求方法都集中在一個特殊的用例上。

$scope.changenote = function(id,note){ 
    $http({method: 
     'POST', 
     url: './dbscripts/solicitors/changenote.php?id=' + id + '&user=' + AppStorage.getCurrentUser().username + '&note=' + encodeURIComponent(note)} 
     ).success(function(data) { 
      $http({ 
       method: 'POST', 
       url: './dbscripts/solicitors/gethistory.php?ref=' + $scope.bbref 
      }).success(function(data) { 
       $scope.histnoteaddbtn = []; 
       $scope.history = data; 
       $state.go($state.current, {}, { 
        reload: true 

       }); 
      }); 
      swal("Saved!", "Your note has been saved!", "success") 
    }) 
}; 
+0

感謝隊友,我不確定這是多麼容易。我有一個問題發送真正的大字符串到數據庫。惡夢! –

+0

@AlexBanerjee歡迎您。您可能會將數據解析爲請求正文而不是網址。該URL有許多限制。 – lin

+0

我有點不確定這是如何完成的。我認爲這就是'POST'這就是爲什麼我想從'GET'切換。我下週開始上課,希望我可以多學點東西。一次性拋出angular,html和css是有點過分了xD –