javascript HTML 编码 ° 度数符号额外空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3808488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:19:57  来源:igfitidea点击:

HTML encoding ° degree symbol extra space

javascripthtmlencodinglocalizationinternationalization

提问by Zac

  1. <div id="a">°F</div>
  2. $.get("http://blah.com/go",{'TU':$('#a').text()});
  3. IIS server logs show the following params:
    99.5% of the time: TU=%C2%B0F
    0.5% of the time: TU=%C2%B0+F
  4. server subsequently crashes because it doesn't know what '° F' is. Admittedly one of the flaws is that we are scraping text out of the DOM & sending it to our server. This is where I suspect the problem is, but I would like to understand more.
  1. <div id="a">°F</div>
  2. $.get("http://blah.com/go",{'TU':$('#a').text()});
  3. IIS 服务器日志显示以下参数:
    99.5% 的时间:TU=%C2%B0F
    0.5% 的时间:TU=%C2%B0+F
  4. 服务器随后崩溃,因为它不知道“° F”是什么。不可否认,其中一个缺陷是我们正在从 DOM 中抓取文本并将其发送到我们的服务器。这是我怀疑问题所在,但我想了解更多。

Other info: the 0.5% of the time has been both IE8 & Chrome. All IP's geolocated to Columbia, which makes it seem like a local issue, but we've been unable to replicate it.

其他信息:0.5% 的时间是 IE8 和 Chrome。所有 IP 都在地理定位到哥伦比亚,这使它看起来像是一个本地问题,但我们一直无法复制它。

Ideas??

想法??

回答by Greg

So the problem is that sometimes there is a space between the °and the F, that space gets translated into a +, and the server doesn't accept it? If so, why not strip out the space before sending it?

所以问题是有时在°和之间有一个空格F,该空格被转换为 a +,而服务器不接受它?如果是这样,为什么不在发送之前去掉空格?

$.get("http://blah.com/go",{'TU':$('#a').text().replace(' ', '')});
// Or a more granular fix
$.get("http://blah.com/go",{'TU':$('#a').text().replace(/°\sF/, '°F')});

回答by Juan Mendes

How is the text being put into the div? You should output that before checking the server value. I don't think it's likely that you're getting a different encoding of the same text. It's probably something to do with how you're putting it into the page.

文本是如何放入 div 中的?您应该在检查服务器值之前输出它。我认为您不太可能获得相同文本的不同编码。这可能与您如何将其放入页面有关。

Also try setting the page encoding on the server before you get the query string, it could be that different browsers are using a different encoding. UTF-8 is the encoding suggested by w3.org. In Java, you have to make sure you set the encoding before any calls to read anything from the client.

还可以尝试在获取查询字符串之前在服务器上设置页面编码,可能是不同的浏览器使用不同的编码。UTF-8 是 w3.org 建议的编码。在 Java 中,您必须确保在任何从客户端读取任何内容的调用之前设置编码。