Ser*_*Amo 42 ajax jquery json character-encoding
在我的网络应用程序中,我使用jQuery的$.getJSON()方法提交了一些表单字段.我在编码方面遇到了一些问题.我的应用程序的字符集是charset=ISO-8859-1,但我认为这些字段是提交的UTF-8.
如何设置$.getJSON调用中使用的编码?
Pat*_*Pat 44
如果您想使用$.getJSON(),可以在通话前添加以下内容:
$.ajaxSetup({
scriptCharset: "utf-8",
contentType: "application/json; charset=utf-8"
});
Run Code Online (Sandbox Code Playgroud)
你可以使用你想要的字符集代替utf-8.
contentType :将数据发送到服务器时,请使用此选项content-type.默认是application/x-www-form-urlencoded,对大多数情况来说都没问题.
scriptCharset :仅适用于具有jsonp或scriptdataType和GET类型的请求.强制请求被解释为某个字符集.只需要远程和本地内容之间的字符集差异.
你可能需要一个或两个......
tra*_*vis 35
我认为$.ajax()如果你想改变编码你可能不得不使用,请参阅contentType下面的参数(success和error回调假设你有<div id="success"></div>和<div id="error"></div>在html中):
$.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);
}
});
Run Code Online (Sandbox Code Playgroud)
我实际上只需要在一个小时前做到这一点,真是巧合!
小智 5
您需要使用Wireshark分析JSON调用,因此您将看到是否在JSON页面的构成中包含了字符集,例如:
0000 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d HTTP / 1.1 200确定。 0010 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74。内容-类型: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控制:无缓冲
0000 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d HTTP / 1.1 200确定。 0010 0a 43 61 63 68 65 2d 43 6f 6e 74 72 6f 6c 3a 20 .Cache-C控制: 0020 6e 6f 2d 63 61 63 68 65 0d 0a 43 6f 6e 74 65 6e无缓存..内容 0030 74 2d 54 79 70 65 3a 20 74 65 78 74 2f 68 74 6d t-类型:文本/ htm 0040 6c 3b 20 63 68 61 72 73 65 74 3d 49 53 4f 2d 38 l; 字符et = ISO-8 0050 38 35 39 2d 31 0d 0a 43 6f 6e 6e 65 63 74 69 6f 859-1..C
这是为什么?因为我们不能将这样的目标放在JSON页面上:
就我而言,我使用制造商Connect Me 9210 Digi:
它对我有用,而不必转换JSON传递的UTF-8数据,然后在页面上重做转换...
| 归档时间: |
|
| 查看次数: |
121684 次 |
| 最近记录: |