jQuery $ .get()没有标头设置的回复的字符集? [英] jQuery $.get() charset of reply when no header is set?

查看:102
本文介绍了jQuery $ .get()没有标头设置的回复的字符集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了 Winamp Song Requester ,这是一个Winamp网络歌曲请求者插件一个内置的最小的HTTP CGI服务器。

I just recently installed Winamp Song Requester wich is a Winamp web song requester plugin with a built in minimal HTTP CGI Server.

插件做的是运行一个web服务器,提供一个html页面与一些特殊的变量,请求(播放列表,请求队列,歌曲剩余时间等)。

What the plugin does is that it runs a web server, serves a html page with some special variables wich it replaces with actual data on request (playlist, request queue, time left in song etc).

我认为这是一个有趣的好项目,学习一些jQuery,所以我开始挂钩我自己的js代码,以替换,修复和ajaxify服务的网站从插件,但我现在遇到一个字符编码的问题。

I saw this as a fun and good project to learn some jQuery so I started hooking up my own js code to replace, fix and ajaxify the served website from the plugin but I've now run into a problem with character encoding.

在页面上,你获得链接到所有的歌曲在播放列表中。当你点击其中一个链接我挂钩了我自己的jQuery点击功能。所以,不是重新加载整个页面,当你请求一首歌我做一个$ .get($(this).attr('href',function(response){... code ...})然后我使用replaceWith用新的生成的队列替换当前队列,你的请求添加在飞行中我做同样的事情来显示/更新当前播放和搜索,以便一切都获取在后台,然后替换为一些动画添加。

On the page you get links to all songs in the playlist. When you click on one of the links I hooked up my own jQuery click function. So instead of reloading the whole page when you request a song I do a $.get($(this).attr('href', function(response) {... code ...}) and then I use replaceWith to replace the current queue with the new generated queue with your request added on the fly. I do the same thing to show/update currently playing and on search so that everything get fetched in the background and then replaced on the fly with some animations added.

所有的jQuery / Ajax都很棒,但我遇到的大问题是使用charset和在队列/播放列表中的歌曲名称。特殊字符(åäöé等)

All jQuery/Ajax works great but the big problem I have is with charset and with song names in queue/playlist. Special characters (åäöé etc.) in names doesn't work at all.

插件输出iso-8859-1 / latin1中的所有内容,标记中的元标记告诉浏览器这个页面是latin1。正常的页面刷新在浏览器这个工作以及特殊字符显示正常。但是当我使用jQuery和$ .get()来替换代码块在飞行中的特殊字符只显示为?。

The plugin outputs everything in iso-8859-1/latin1 and my meta tag in the markup tells the browser that this page is latin1. On a normal page refresh in the browser this works well and the special characters display as normal. But when I use jQuery and $.get() to replace blocks of code on the fly the special characters only show up as ?.

我认为问题在于jQuery默认认为$ .get()响应是UTF-8,如果没有标题说。该插件根本没有设置任何编码/字符集头,因为我没有控制所有的后端和什么头设置我不能改变这一点。

I think that the problem lies in that jQuery defaults to believe that the $.get() response is UTF-8 if no header says otherwise. The plugin doesn't set any header for encoding/charset at all and since I have no control at all of the backend and what headers get set I can't change this.

我从插件的响应中获得的唯一标头是:

The only headers I get in the response from the plugin is:


服务器:WinampServerogle
连接: close

Content-Type:text / html

Server: WinampServer
Connection: close
Content-Type: text/html

我希望你了解我的问题。我有一个页面,我在后端没有控制,所有我需要处理的是生成的HTML。我无法在回复中更改或添加标题。我需要告诉jQuery的响应实际上是在latin1而不是UTF-8,以便特殊字符的编码不破。我试过scriptCharset:'iso-8859-1'在jQuerys ajaxSetup,但只适用于类型脚本/ json和我使用HTML响应。

I hope you understand my problem. I've got a page where I have no control at all over the backend and all I have to work with is generated HTML. I can't change or add headers in responses. I need to tell jQuery that the response is actually in latin1 and not UTF-8 so that the encoding of special characters don't break. I've tried the scriptCharset: 'iso-8859-1' in jQuerys ajaxSetup but that only works with type script/json and I'm working with HTML responses.

推荐答案

编辑:ok我想这个工作(至少它在我的测试环境中工作,看到以前的尝试的修订)

edit: ok i think this works (at least it worked in my test environment, see revisions for previous attempt)

$.ajaxSetup({
    'beforeSend' : function(xhr) {
        xhr.overrideMimeType('text/html; charset=UTF-8');
    },
});
$('#stuff').load('/yourresource.file'); // your ajax load

我在 -8 和在 ISO-8859-1 中设置的数据文件。没有上面的代码,我得到一堆垃圾的测试字符串åäöé,如预期。与上述代码,它加载åäöé正确编码。

what i had was the main file set in UTF-8 and the data file set in ISO-8859-1. without the above code, i got a bunch of garbage for the test string åäöé, as expected. with the above code, it loaded åäöé properly encoded.

这篇关于jQuery $ .get()没有标头设置的回复的字符集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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