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

查看:19
本文介绍了未设置标题时的 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.

该插件的作用是运行一个网络服务器,提供一个带有一些特殊变量的 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 都很好用,但我遇到的大问题是字符集和队列/播放列表中的歌曲名称.名称中的特殊字符(åäöé 等)根本不起作用.

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:

服务器:WinampServer
连接:关闭
内容类型:text/html

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

我希望你能理解我的问题.我有一个页面,我完全无法控制后端,我需要处理的只是生成的 HTML.我无法在响应中更改或添加标题.我需要告诉 jQuery 响应实际上是 latin1 而不是 UTF-8,这样特殊字符的编码就不会中断.我已经在 jQuery ajaxSetup 中尝试了 scriptCharset: 'iso-8859-1' 但它只适用于类型 script/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.

知道这是否可行或您能想到的任何其他解决方法吗?

Any idea if this is possible or any other workaround you could think about?

推荐答案

edit:好的,我认为这有效(至少它在我的测试环境中有效,请参阅先前尝试的修订版)

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

我拥有的是在 UTF-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天全站免登陆