JS encodeURIComponent结果与FORM创建的结果不同 [英] JS encodeURIComponent result different from the one created by FORM

查看:149
本文介绍了JS encodeURIComponent结果与FORM创建的结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为在表单中输入的值是由浏览器正确编码的。

I thought values entered in forms are properly encoded by browsers.

但是这个简单的测试文件test_get_vs_encodeuri.html显示它不是真的:

But this simple test file "test_get_vs_encodeuri.html" shows it's not true:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
   <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
   <title></title>
</head><body>

<form id="test" action="test_get_vs_encodeuri.html" method="GET" onsubmit="alert(encodeURIComponent(this.one.value));">
   <input name="one" type="text" value="Euro-€">
   <input type="submit" value="SUBMIT">
</form>

</body></html>

点击提交按钮时:

encodeURICompenent将输入值编码为Euro-%E2%82%AC

,而浏览器进入GET查询只写一个简单的欧元 - %80


  1. 有人可以解释一下吗?

  1. Could someone explain?

我如何使用Javascript ???()以相同的方式对borwser的FORM(windows-1252)进行编码?(escape函数不起作用,encodeURIComponent也不起作用)?

How do i encode everything in the same way of the borwser's FORM (windows-1252) using Javascript??? (escape function does not work, encodeURIComponent does not work either)?

或者encodeURIComponent进行不必要的转换?

Or is encodeURIComponent doing unnecessary conversions?

推荐答案

这是一个字符编码问题。您的文档正在使用charset Windows-1252 ,其中位于128位,用Windows-1252编码为0x80。但 encodeURICompenent 期望输入为UTF-8,因此使用Unicode的字符集,其中位于第8364位( PDF )用UTF-8 0xE282AC编码。

This is a character encoding issue. Your document is using the charset Windows-1252 where the is at position 128 that is encoded with Windows-1252 as 0x80. But encodeURICompenent is expecting the input to be UTF-8, thus using Unicode’s charset where the is at position 8364 (PDF) that is encoded with UTF-8 0xE282AC.

解决方案是将UTF-8用于您的文档。或者你编写一个映射来将UTF-8编码的字符串转换为Windows-1252。

A solution would be to use UTF-8 for your document as well. Or you write a mapping to convert UTF-8 encoded strings to Windows-1252.

这篇关于JS encodeURIComponent结果与FORM创建的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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