如何在 .getJSON jQuery 中设置编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:40:24  来源:igfitidea点击:

How to set encoding in .getJSON jQuery

jqueryajaxjsoncharacter-encoding

提问by Sergio del Amo

In my web app, I submit some form fields with jQuery's $.getJSON()method. I am having some problems with the encoding. The character-set of my app is charset=ISO-8859-1, but I think these fields are submitted with UTF-8.

在我的 Web 应用程序中,我使用 jQuery 的$.getJSON()方法提交了一些表单字段。我在编码方面遇到了一些问题。我的应用程序的字符集是charset=ISO-8859-1,但我认为这些字段是用UTF-8.

How I can set encoding used in $.getJSONcalls?

如何设置$.getJSON呼叫中使用的编码?

采纳答案by travis

I think that you'll probably have to use $.ajax()if you want to change the encoding, see the contentTypeparam below (the successand errorcallbacks assume you have <div id="success"></div>and <div id="error"></div>in the html):

我认为$.ajax()如果您想更改编码,您可能必须使用,请参阅contentType下面的参数(successerror回调假设您在 html 中拥有<div id="success"></div><div id="error"></div>):

$.ajax({
    type: "POST",
    url: "SomePage.aspx/GetSomeObjects",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{id: '" + someId + "'}",
    success: function(json) {
        $("#success").html("json.length=" + json.length);
        itemAddCallback(json);
    },
    error: function (xhr, textStatus, errorThrown) {
        $("#error").html(xhr.responseText);
    }
});

I actually just had to do this about an hour ago, what a coincidence!

实际上,大约一个小时前我不得不这样做,真是巧合!

回答by Pat

If you want to use $.getJSON()you can add the following before the call :

如果要使用$.getJSON(),可以在调用前添加以下内容:

$.ajaxSetup({
    scriptCharset: "utf-8",
    contentType: "application/json; charset=utf-8"
});

You can use the charset you want instead of utf-8.

您可以使用所需的字符集而不是utf-8.

The options are explained here.

此处解释这些选项。

contentType :When sending data to the server, use this content-type. Default is application/x-www-form-urlencoded, which is fine for most cases.

contentType :向服务器发送数据时,请使用此content-type. 默认为application/x-www-form-urlencoded,这适用于大多数情况。

scriptCharset :Only for requests with jsonpor scriptdataType and GET type. Forces the request to be interpreted as a certain charset. Only needed for charset differences between the remote and local content.

scriptCharset :仅适用于jsonpscriptdataType 和 GET 类型的请求。强制将请求解释为某个字符集。仅需要远程和本地内容之间的字符集差异。

You may need one or both ...

您可能需要一种或两种...

回答by Felipe Alcará

You need to analyze the JSON calls using Wireshark, so you will see if you include the charset in the formation of the JSON page or not, for example:

您需要使用 Wireshark 分析 JSON 调用,因此您将查看是否在 JSON 页面的格式中包含字符集,例如:

  • If the page is simple if text / html
  • 如果页面很简单 if text / html
0000  48 54 54 50 2f 31 2e 31  20 32 30 30 20 4f 4b 0d   HTTP/1.1  200 OK.
0010  0a 43 6f 6e 74 65 6e 74  2d 54 79 70 65 3a 20 74   .Content -Type: t
0020  65 78 74 2f 68 74 6d 6c  0d 0a 43 61 63 68 65 2d   ext/html ..Cache-
0030  43 6f 6e 74 72 6f 6c 3a  20 6e 6f 2d 63 61 63 68   Control:  no-cach
  • If the page is of the type including custom JSON with MIME "charset = ISO-8859-1"
  • 如果页面的类型包括带有 MIME“charset = ISO-8859-1”的自定义 JSON
0000  48 54 54 50 2f 31 2e 31  20 32 30 30 20 4f 4b 0d   HTTP/1.1  200 OK.
0010  0a 43 61 63 68 65 2d 43  6f 6e 74 72 6f 6c 3a 20   .Cache-C ontrol: 
0020  6e 6f 2d 63 61 63 68 65  0d 0a 43 6f 6e 74 65 6e   no-cache ..Conten
0030  74 2d 54 79 70 65 3a 20  74 65 78 74 2f 68 74 6d   t-Type:  text/htm
0040  6c 3b 20 63 68 61 72 73  65 74 3d 49 53 4f 2d 38   l; chars et=ISO-8
0050  38 35 39 2d 31 0d 0a 43  6f 6e 6e 65 63 74 69 6f   859-1..C onnectio

Why is that? because we can not put on the page of JSON a goal like this:

这是为什么?因为我们不能在 JSON 页面上放置这样的目标:

In my case I use the manufacturer Connect Me 9210 Digi:

就我而言,我使用制造商 Connect Me 9210 Digi:

  • I had to use a flag to indicate that one would use non-standard MIME: p-> theCgiPtr-> = fDataType eRpDataTypeOther;
  • It added the new MIME in the variable: strcpy (p-> theCgiPtr-> fOtherMimeType, "text / html; charset = ISO-8859-1 ");
  • 我不得不使用一个标志来表明人们将使用非标准 MIME: p-> theCgiPtr-> = fDataType eRpDataTypeOther;
  • 它在变量中添加了新的 MIME:strcpy (p-> theCgiPtr-> fOtherMimeType, "text / html; charset = ISO-8859-1 ");

It worked for mewithout having to convert the data passed by JSON for UTF-8 and then redo the conversion on the page ...

它对我有用,而无需将 JSON 传递的数据转换为 UTF-8,然后在页面上重做转换......

回答by zahra salmaninejad

f you want to use $.getJSON() you can add the following before the call :

如果你想使用 $.getJSON() 你可以在调用之前添加以下内容:

$.ajaxSetup({
    scriptCharset: "utf-8",
    contentType: "application/json; charset=utf-8"
});

回答by Anderson Mao

Use encodeURI()in client JS and use URLDecoder.decode()in server Java side works.

使用encodeURI()在客户端的JS和使用URLDecoder.decode()在服务器端的Java作品。



Example:

例子:

  • Javascript:

    $.getJSON(
        url,
        {
            "user": encodeURI(JSON.stringify(user))
        },
        onSuccess
    );
    
  • Java:

    java.net.URLDecoder.decode(params.user, "UTF-8");

  • Javascript:

    $.getJSON(
        url,
        {
            "user": encodeURI(JSON.stringify(user))
        },
        onSuccess
    );
    
  • 爪哇

    java.net.URLDecoder.decode(params.user, "UTF-8");

回答by Vipin Kohli

Use this function to regain the utf-8 characters

使用此函数重新获得 utf-8 字符

function decode_utf8(s) { 
  return decodeURIComponent(escape(s)); 
}

Example: var new_Str=decode_utf8(str);

示例:var new_Str=decode_utf8(str);