如何在http.post url中传递像“#”,“^”这样的特殊字符作为参数

时间:2018-03-16 07:46:01

标签: javascript angularjs

#中传递^http.post url等特殊字符时,它会返回错误请求。

$http.post("requestFormDataInsert.jsp?manager=" +$scope.managerName+ "&productName="+ $scope.productName+ "&productVersion="+ $scope.versionNumber+ "&expectedDate="+ $("#datepicker1").val()+  "&description=" +$scope.description+ "&requestType="+$scope.requestType ).then( function( resp ){
    $scope.requestId = resp.data;
    alert("Your response has been updated successfully")
    $location.path('/');
});

假设我们在这里有一些参数,其中包含#(哈希)或^字符,因此它表示为bad request

1 个答案:

答案 0 :(得分:3)

你需要编码你的参数。您可以使用encodeURI手动对其进行编码,也可以将其添加到请求的param部分。通过这种方式,您的参数会自动进行序列化和编码。

$http({
  url: 'requestFormDataInsert.jsp',
  method: 'POST',
  params: {
    manager: $scope.managerName,
    productName: $scope.productName,
    productVersion: $scope.versionNumber,
    expectedDate: $("#datepicker1").val(),
    description: $scope.description,
    requestType: $scope.requestType
  }
}).then(function (result) {
});