javascript - 我如何在 http.post url 中传递特殊字符,如 "#"、 "^"作为参数

标签 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('/');
});

假设我们这里有一些参数,其中包含 # (哈希)或 ^ 字符,因此它表明错误请求

最佳答案

您需要对您的参数进行编码。您可以使用 encodeURI 手动对它们进行编码或者只是根据您的要求将它们添加到参数部分。这样你的参数就会自动序列化和编码。

$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) {
});

关于javascript - 我如何在 http.post url 中传递特殊字符,如 "#"、 "^"作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49315492/

相关文章:

javascript - 单击功能中输入字段值不变

javascript - 如何从 <script> 标签调用 javascript 函数?

c# - "Incorrect Content-Type: "异常抛出 Angular mvc 6 应用程序

javascript - api调用完成后 ionic 隐藏启动画面

javascript - 如何在新的 Error 对象 Javascript 的消息中添加换行符?

javascript - 选择基础元素?

javascript - 我如何在 javascript 中反向迭代对象的属性?

javascript - 注册后 req.user 未定义

javascript - 来自嵌套自定义指令的 Angular 调用 Controller 函数

javascript - ng-class 在一个 ng-class 指令中具有条件和函数