无法在标头中发送特殊字符

时间:2017-03-22 10:57:55

标签: javascript angularjs http-headers

我正在尝试使用以下代码从javascript中在标头中发送一些特殊字符:

  $scope.validatePass = function (pincode,userName) {
                httpCallConfig.headers = angular.extend(httpCallConfig.headers || {}, {
                    "Re-Authenticate-Me": "Yes"
                    , "j_username": userName
                    , "j_password": pincode
                });
                $http(httpCallConfig).then(
                    function (resps) {
                        if (resps === false) {
                            return;
                        }
                        $modalInstance.close(resps);
                    }
                    , function (data) {
                        if (data.status == 401) {
                           $log.info("faile pincode");
                        } else {
                            $log.error(data);
                            $scope.error = "other error!";
                        }
                    }
                );
            };

但如果userName包含任何特殊字符å,ø,æ,则会出现问题,然后服务器返回401状态。如果我需要发送特殊字符,有人可以告诉我如何以及需要使用哪种编码。

1 个答案:

答案 0 :(得分:0)

它只需要encodeURI:

 "j_username": encodeURI(userName)