0

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);
ozgen
  • 21
  • 1
  • 6

1 Answers1

-1

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
});
Ali Baig
  • 3,819
  • 4
  • 34
  • 47
  • I refactor my code according to your suggession but the error is not solved @Ali Baig – ozgen Jan 26 '17 at 00:08
  • Yes exactly the same : Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'ŞanlıUrfa' is not a valid HTTP header field value. @Ali Baig – ozgen Jan 26 '17 at 00:12
  • Where are you setting ŞanlıUrfa header? – Ali Baig Jan 26 '17 at 00:14
  • In the getHeader( ) function : 'region': String($cookieStore.get('loggedin').roles[0].region) '$cookieStore.get('loggedin').roles[0].region' value is ŞanlıUrfa – ozgen Jan 26 '17 at 00:15
  • Try this http://stackoverflow.com/questions/12190166/angularjs-any-way-for-http-post-to-send-request-parameters-instead-of-json – Ali Baig Jan 26 '17 at 00:22
  • I have tried this solution before. However i get an error from body-parser in express server. – ozgen Jan 26 '17 at 00:27