简体   繁体   中英

how set charset utf-8 to header to $http GET and POST in angular js

My service functions are :

function getHeader() {

            return {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                    'authorization': $cookieStore.get('loggedin').token,
                    'region': String($cookieStore.get('loggedin').roles[0].region),
                    'branch': String($cookieStore.get('loggedin').roles[0].branch)
                }
            };
        }


function getCompletedPlateReq() {
        var deferred = $q.defer();
        $http.get('/client/get/reqplate/completed', {}, getHeader()).success(function (data) {
            if (data) deferred.resolve(data);
        }).error(function (err) {
            if (err) deferred.reject();
        });

        return deferred.promise;

    }

These are PlateService functions and when i try to call getCompletedPlateReq() function like:

PlateService.getCompletedPlateReq().then(function (data) {
        data.platePocess = 'completed';
        $scope.completedPlates = data;
        setDataToPnl(data);

    })

I get this error :

Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'ŞanlıUrfa' is not a valid HTTP header field value.
at angular.js:10990
at forEach (angular.js:355)
at angular.js:10988
at sendReq (angular.js:10841)
at serverRequest (angular.js:10551)
at processQueue (angular.js:15122)
at angular.js:15138
at Scope.$eval (angular.js:16384)
at Scope.$digest (angular.js:16200)
at Scope.$apply (angular.js:16492)

I add 'Content-type' to getHeader() function but it did not work properly because of the Turkish characters. When I change the value of 'ŞanlıUrfa' to 'SanliUrfa' from mongoDB, the service work properly. But I want to use Turkish characters in the headers. how can i handle this issue. thanks for help...

my backend function in PlateBusiness :

exports.getCompletedPlateReq = function (req, res) {
const query = PlateRequest.find();
query.where('process').equals('produced_montaged');
query.where('is_deleted').ne(1);
if (req.headers.region !== undefined)
    query.where('region').equals(req.headers.region);
if (req.headers.branch !== undefined)
    query.where('branch').equals(req.headers.branch);
query.where('status').equals(1);

query.exec(function (err, data) {
    if (err) return res.status(500).send(err);
    return res.status(200).send(data);

});

}

and the router part is :

app.get('/get/reqplate/completed', requireAuth, PlateBusiness.getCompletedPlateReq);

It seems that you need to pass something in the body to work with the header's content type. Give it a try like this.

$http({
    url: '/client/get/reqplate/completed',    
    method: 'POST',
    data: '',
    headers: {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
         ...
    }

}).success(function(response){
    //consume success response
}).error(function(error){
    //consume error response
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM