如何在 .getJSON jQuery 中设置编码 [英] How to set encoding in .getJSON jQuery

查看:43
本文介绍了如何在 .getJSON jQuery 中设置编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如何设置在 $.getJSON 调用中使用的编码?

How I can set encoding used in $.getJSON calls?

推荐答案

我认为你可能不得不使用 $.ajax() 如果要更改编码,请参阅下面的 contentType 参数(successcode> 和 error 回调假设你有 <div id="success"></div><div id="error"></div> 在 html):

I think that you'll probably have to use $.ajax() if you want to change the encoding, see the contentType param below (the success and error callbacks assume you have <div id="success"></div> and <div id="error"></div> in the 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);
    }
});

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

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

这篇关于如何在 .getJSON jQuery 中设置编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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