angular.js $ http.get如何强制UTF-8编码 [英] angular.js $http.get how to force UTF-8 encoding

查看:1255
本文介绍了angular.js $ http.get如何强制UTF-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读JSON文件连接codeD使用UTF-8字符集结果
我用这个语法:

  $ http.get('资源/ negozi.json',
    {头:{内容类型:应用/ JSON的;字符集= UTF-8'}
})。成功(功能(数据){
... code在这里
});

不过,响应报头是:


  

内容类型:text / plain的;字符集= ISO-8859-1


如果我试图用jQuery做到这一点:

  $。阿贾克斯({
键入:GET,
网址:资源/ negozi.json
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON

请求头是正确的。但是,该反应是相同的。


  

内容类型:text / plain的;字符集= ISO-8859-1



解决方案

看起来你可能需要设置一个接受头。在的contentType 头适用于您要发送的东西;

 接受:应用/ JSON的;字符集= UTF-8
接收字符集:UTF-8

左右;

  $。阿贾克斯({
   键入:GET,
   网址:资源/ negozi.json
   标题:{
       接受:应用/ JSON的;字符集= UTF-8,
       接收字符集:字符集= UTF-8
   },
   数据类型:JSON
});

I've to read JSON file encoded with utf-8 charset
I use this syntax:

$http.get('resources/negozi.json',
    {header : {'Content-Type' : 'application/json; charset=UTF-8'}
}).success(function(data) {
... code here
});

But, the response header is:

Content-Type:text/plain; charset=ISO-8859-1

If I try to do this with jquery:

$.ajax({
type: "GET",
url: "resources/negozi.json",
contentType: "application/json; charset=utf-8",
dataType: "json",

The request header is correct. But, the response is the same.

Content-Type:text/plain; charset=ISO-8859-1

解决方案

It looks like you might need to set an Accept header. The contentType header applies to the stuff you're sending;

Accept: application/json;charset=UTF-8
Accept-Charset: UTF-8

so;

$.ajax({
   type:"GET",
   url:"resources/negozi.json",
   headers: {
       "Accept": "application/json;charset=utf-8",
       "Accept-Charset":"charset=utf-8"
   },
   dataType:"json"
});      

这篇关于angular.js $ http.get如何强制UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆