读取编码的URL

huangapple go评论74阅读模式
英文:

Read encoded url

问题

以下是要翻译的内容:

不工作的部分:

var text = '{ "data":[ ' +
  '{ "name": "Gerschäftsfü' +
  'chrer"}' +
  ']}';
document.getElementById("test").innerHTML = text;

工作的部分:

var text = '{ "data":[ ' +
  '{ "name": "Gerschäftsfü' +
  'hrer"}' +
  ']}';
document.getElementById("test").innerHTML = text;

如果将字符ü传递为%C3%BC,它可以正常工作。

英文:

Can someone explain why the first code doesn't work and the second does. The only difference is that the string is split in a different place.

Doesn't work:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var text = &#39;{&quot;data&quot;:[&#39; +
  &#39;{&quot;name&quot;: &quot;Gersch\u00e4ftsf\u00f&#39; +
  &#39;chrer&quot;}&#39; +
  &#39;]}&#39;;
document.getElementById(&quot;test&quot;).innerHTML = text;

<!-- language: lang-html -->

&lt;p id=&quot;test&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

This works fine:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var text = &#39;{&quot;data&quot;:[&#39; +
  &#39;{&quot;name&quot;: &quot;Gersch\u00e4ftsf\u00fc&#39; +
  &#39;hrer&quot;}&#39; +
  &#39;]}&#39;;
document.getElementById(&quot;test&quot;).innerHTML = text;

<!-- language: lang-html -->

&lt;p id=&quot;test&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

I get it working if I pass the character &#252; not as \u00fc but as %C3%BC

答案1

得分: 2

问题在于你在Unicode转义序列的中间对字符串进行了拆分。当你使用+运算符连接字符串时,会将它们连接在一起,但是在连接之前会解析Unicode转义序列。

所以,在你的第一个代码中,它试图解析序列\u00f,这是不可能的,因为Unicode转义序列需要四个字符长。

在你的第二个代码中,你使用了完整的序列\u00fc,这是正确的。

英文:

The issue is that you're splitting the string in the middle of a Unicode escape sequence. When you use the + operator with strings, you concatenate them, but Unicode escape sequences are resolved before concatenation.

So, in your first code, it's trying to resolve the sequence \u00f, which is impossible because Unicode escape sequences need to be four characters long.

In your second code, you use the full sequence \u00fc which is correct.

答案2

得分: 1

转义序列(例如\u00fc)在解析字符串字面值时会转换为字符(即从源代码转换为字符串)。

如果你将一个转义序列的一半放在一个字符串字面值中,将另一半放在另一个字符串字面值中(然后尝试使用+运算符将这两部分连接在一起),那么在JavaScript尝试将其转换为字符时,转义序列将不完整。

英文:

Escape sequences (such as \u00fc) are converted to characters when the string literal is parsed (i.e. converted from source code to a string).

If you put half of an escape sequence in one string literal and half in another string literal (and then attempt to join the two parts together with the + operator) then the escape sequence is incomplete at the time JavaScript tries to convert it into a character.

答案3

得分: 1

控制台错误明确告诉您问题所在:

> 无效的Unicode转义序列

这是一个无效的Unicode字符:

\u00f

但这个是有效的:

\u00fc

字符串的其余部分,例如如何分割它,都不相关。 Unicode字符的语法必须在使用它的字符串文字中正确。

这个文字是无效的:

'{"name": "Gersch\u00e4ftsf\u00f'

因此,任何试图使用这个文字的JavaScript代码都会因为无效的Unicode语法而失败。无论这段代码是否打算尝试将另一个字符串文字附加到这个字符串文字上,因为这个字符串文字已经损坏,所以不能用于任何其他目的。

就像在工作版本中一样,将字符串分割成每个单独的字符串文字都是有效的字符串文字。

英文:

The console error is telling you exactly the problem:

> Invalid Unicode escape sequence

This is an invalid unicode character:

\u00f

But this one is valid:

\u00fc

The rest of the string, such as how you split it, is irrelevant. The syntax of the unicode character must be correct within the string literal that uses it. This literal is invalid:

&#39;{&quot;name&quot;: &quot;Gersch\u00e4ftsf\u00f&#39;

So any JavaScript code which tries to use this literal will fail because of that invalid unicode syntax. It doesn't matter if that code was going to try to append another string literal to this one, since this string literal is broken it can't be used for that or any other purpose.

Just as in the working version, split the string in such a way that each individual string literal is a valid string literal.

huangapple
  • 本文由 发表于 2023年7月6日 22:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定