ie ajax请求头header无效,IE无法打印响应头(来自ajax)为utf8(IE cant print response header (from ajax) as utf8)...

IE无法打印响应头(来自ajax)为utf8(IE cant print response header (from ajax) as utf8)

我使用PJAX来更改我的网站上的页面,对于每个新页面,我发送一个带有新的Page-Title标题的Response Header标题。 此标头包含可以包含åäö,并且IE输出不正确。 在IE的开发人员工具中,我可以毫无问题地看到Page-Title响应,但是当我使用document.title = ...来更新标题时,它显示错误。

我的回答标题如下: Page-Title: Mårtensson

UTF8已经确认,我可以看到Content-Type: text/html; chartset=UTF-8 Content-Type: text/html; chartset=UTF-8 IE和Chrome的Dev Tools中的Content-Type: text/html; chartset=UTF-8 。

这是我更新标题的方式:

$.ajax({

url: State.url,

type: 'get',

beforeSend: function(xhr){

xhr.setRequestHeader('X-PJAX', true);

},

success: function(resp, status, xhr) {

document.title = xhr.getResponseHeader('Page-Title');

// other code

}

});

这适用于Chrome,但在IE中它输出为MÃ¥rtensson 。 如果我使用decodeURIComponent(escape(document.title = xhr.getResponseHeader('Page-Title'))); 它在IE中输出正常,但在Chrome中我得到Uncaught URIError: URI malformed 。

如何让它在两种浏览器中都能运行?

我通过在字符串上运行htmlentities来修复它,然后将其作为响应头输出。 然后我在javascript中解码字符串,然后将其设置为标题。

document.title = decodeEntities(xhr.getResponseHeader('Page-Title'));

I use PJAX to change pages on my site and for every new page I send a Response Header with a new a Page-Title header. This header contains can contain åäö, and IE outputs this incorrectly. In IE's Developer Tools I can see the Page-Title response without problem, but when I use document.title = ... to update the title it displays wrong.

My response header looks like this: Page-Title: Mårtensson

UTF8 is confirmed, I can see Content-Type: text/html; chartset=UTF-8 in both IE's and Chrome's Dev Tools.

This is how I update the title:

$.ajax({

url: State.url,

type: 'get',

beforeSend: function(xhr){

xhr.setRequestHeader('X-PJAX', true);

},

success: function(resp, status, xhr) {

document.title = xhr.getResponseHeader('Page-Title');

// other code

}

});

This works in Chrome but in IE it outputs as MÃ¥rtensson. If I use decodeURIComponent(escape(document.title = xhr.getResponseHeader('Page-Title'))); it outputs fine in IE, but then in Chrome I get Uncaught URIError: URI malformed.

How can I get it to work in both browsers?

Solution

I fixed it by running htmlentities on the string before I output it as a response header. I then decode the string in javascript before I set it as title.

document.title = decodeEntities(xhr.getResponseHeader('Page-Title'));

原文:https://stackoverflow.com/questions/19704299

更新时间:2020-01-04 21:44

最满意答案

您可以查看专门处理此问题的RFC 5987 。 基本上,请记住,如果在HTTP标头字段中使用UTF-8字符,则您自己就可以使用,不同的浏览器会有不同的行为。

如果你真的需要使用标题,你应该使用某种转义(服务器端)和unescaping(客户端)来使你的标题适合ISO-8859-1字符集。

you can check RFC 5987 that specifically deals with this issue. Basically, keep in mind that if you use UTF-8 characters in a HTTP header field, you are on your own and different browsers will have different behavior.

If you really need to use a header for this, you should use some kind of escaping (server side) and unescaping (client side) to make your title fit into the ISO-8859-1 charset.

相关问答

首先,更正: TestText = "Test - āĀēĒčČ..šŠūŪžŽ" # this NOT utf-8...it is a Unicode string in Python 3.X.

TestText2 = TestText.encode('utf8') # THIS is "just bytes" in UTF-8.

现在,要将UTF-8发送到标准输出,无论控制台的编码如何,请使用正确的工具执行此任务: import sys

sys.stdout.buffer.write(Tes

...

虽然看起来你已经解决了这个问题,但最好指出两件事: jQuery的getJSON默认使用UTF-8。 你给出的页面上接受的答案是,如果你想要一些UTF-8以外的编码,你可以使用$.ajax() 。 实际上,正如该页面上的另一个答案所说,即使您使用getJSON ,您仍然可以使用$.ajaxSetup来设置编码。 您可能希望将JSP标头contentType更改为'application/json; charset=utf-8' 'application/json; charset=utf-8' ,

...

过去我不得不尝试“修复”一些UTF8的破坏情况,不幸的是,这并不容易,而且往往是不可能的。 除非你可以确切地确定它是如何被破坏的,它总是以完全相同的方式破坏,那么这将是很难“撤消”的伤害。 如果您想尝试撤销损害,您最好的打算是开始编写一些示例代码,您可以尝试使用mb_convert_encoding()调用多种变体,以查看是否可以找到“from”和“to”的组合修复您的数据。 最后,由于所涉及的痛苦程度,最好不要担心修复旧的数据,而只是修复未来的事情。 但是,在此之前,您需要确保您首先修复导致此问

...

您可以查看专门处理此问题的RFC 5987 。 基本上,请记住,如果在HTTP标头字段中使用UTF-8字符,则您自己就可以使用,不同的浏览器会有不同的行为。 如果你真的需要使用标题,你应该使用某种转义(服务器端)和unescaping(客户端)来使你的标题适合ISO-8859-1字符集。 you can check RFC 5987 that specifically deals with this issue. Basically, keep in mind that if you use UT

...

不得不在php脚本中的每个变量周围添加utf8_encode($ variable)并且它有效。 Had to put utf8_encode ( $variable ) around each variable in the php script and it worked.

正如anatoly techtonik 指出 FITS标头中的非ASCII字符完全无效,并使无效的FITS文件。 也就是说,如果astropy.io.fits至少可以读取无效条目,那就太好了。 对此的支持目前已经破裂,需要一个冠军才能修复它,但是没有人拥有,因为这是一个不常见的问题,大多数人在一两个文件中遇到它,修复这些文件,然后继续前进。 不过会喜欢有人来解决这个问题。 与此同时,由于你确切地知道这个文件正在打嗝的字符串,我只想以原始二进制模式打开文件并替换字符串。 如果FITS文件非常大,您可

...

您应该手动编码/解码utf-8文件中的数据: reader = csv.reader(inputFile, delimiter=sepChar)

for line in reader:

if (lno == 0 and titlePresent):

if (len(titleCols) == 0):

titleCols = line

writeExcelHeader(worksheet, titleCols)

else:

# writeExcelRow(

...

答案(通过评论达成)是用HttpUtility.UrlEncode替换HttpUtility.UrlEncode 此方法具有将编码作为参数的重载。 The answer (reached via comments) was to replace WebUtility.UrlEncode with HttpUtility.UrlEncode. This method has an overload that takes encoding as a parameter.

将utf8_general_ci转换为utf8_unicode_ci ... 连接数据库后尝试运行SET NAMES UTF8查询... function connect($server, $database, $username, $password, $charset = "UTF8"){

$link = mysql_connect($server, $database, $password);

if(!$link){

die("Unable to connec

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值