Ajax内容类型Unicode问题

时间:2014-02-03 12:37:42

标签: c# javascript ajax unicode

我们有一个字符串česko 这是代码

$.ajax({
    type: 'GET',
    url: stringUrl,
    cache: false,
    contentType: 'application/json; charset=utf-8',
    success: function(response) {
        //on succes
    }
});

当我们通过查询字符串传递时,我们在javascript中收到相同的字符串

但是当我们将上面提到的字符串传递给web api控制器

然后我们在c#代码中收到?esko 字符串而不是česko字符串。

请帮助

1 个答案:

答案 0 :(得分:0)

根据你的评论,你应该真的改变:

var uri = "my test.asp?name=ståle&car=saab"; var res = encodeURI(uri);

这样的事情:

var fileName = "my test.asp";
// You can join the string literals I have below together, but I am
//   splitting them apart here for the sake of the discussion below.
var res = fileName + "?" + "name" + "=" + encodeURIComponent("ståle") +
            "&" + "car" + "=" + encodeURIComponent("saab");

我的上述代码假定变量名称namecar仅为ASCII fileName。如果fileName中可能包含非ASCII字符,则应在其上使用encodeURI(因为它不应该有“?”字符来启动查询字符串),而如果变量< em> names ,如“name”和“car”实际上有非ASCII字符,你应该使用encodeURIComponent(但不能使用“?”,“&amp;”和“=”我的代码中包含的字符。)

您在非ASCII部分需要encodeURIComponent()的原因是因为这样可以确保“=”,“&amp;”和“?”之类的字符转义而不是允许拆分查询字符串。