简体   繁体   English

如何在角度js中将charset utf-8设置为$ http GET和POST的标头

[英]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()函数时,例如:

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. 我在getHeader()函数中添加了“ Content-type”,但由于土耳其字符而无法正常工作。 When I change the value of 'ŞanlıUrfa' to 'SanliUrfa' from mongoDB, the service work properly. 当我从mongoDB将'ŞanlıUrfa'的值更改为'SanliUrfa'时,该服务正常工作。 But I want to use Turkish characters in the headers. 但是我想在标题中使用土耳其语字符。 how can i handle this issue. 我该如何处理这个问题。 thanks for help... 感谢帮助...

my backend function in PlateBusiness : 我在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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将动态形式的字符集设置为UTF-8? - How set the charset of dynamic form to UTF-8? Angular 4 http.get接收“字符集UTF-8”,但将重音符号更改为其对应的ISO代码 - Angular 4 http.get receive “charset UTF-8” but change accented character to their ISO code counterpart angular.js $ http.get如何强制UTF-8编码 - angular.js $http.get how to force UTF-8 encoding 如何在javascript文件中设置charset =“utf-8” - How to set charset=“utf-8” in the javascript file itself 如何使用在Content-Type标头中同时具有“ application / x-www-form-urlencoded”和“ charset = UTF-8”的Javascript发布HTML表单 - How to post a HTML form using Javascript that has both “application/x-www-form-urlencoded” and “charset=UTF-8” in the Content-Type header 当我在js文件中设置charset =“ utf-8”时出现意外的令牌&lt;异常 - Unexpected token < exception when I set charset=“utf-8” in js file POST数据将始终使用UTF-8字符集传输到服务器 - POST data will always be transmitted to the server using UTF-8 charset 在jQuery.getScript()中,我们如何将字符集类型设置为utf-8? - In jQuery.getScript(), how do we set the charset type to utf-8? 将标题授权设置为有角$ http post - set header authorization to angular $http post 如何将 HTTP 响应主体编码为 node.js 中的 UTF-8 - How to encode HTTP response body as UTF-8 in node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM