使用 jquery 发布和返回数据会损坏 å ä ö
这个问题网上有很多资料。尽管如此,我还是没有解决。
我在网页上有一个动态加载的表单。我使用 jquery $.post 提交它。我的页面面向瑞典人,因此需要与 åäö 配合使用。
当将数据发布到服务器时,它会将数据加载回新表中。返回的 å ä ö 已损坏。示例 åååäääööö = åååäääööö = åååäääööö = ¤¤¤à¶¶¶
服务器端是 php。我对所有返回内容到浏览器的页面进行编码,如下所示:
<?php header('Content-type: text/html; charset=ISO-8859-1'); ?>
我在服务器端尝试过 utf8_encode 和 utf8_decode 。实际上没有区别。
我尝试了这个:encodeURIComponent($(this).serialize()) 其中$(this) 是要提交的表单。当然,这是行不通的,我不知道如何对像 $(this) 这样的对象进行编码。
我认为这是很多人都会做的事情,但它似乎并不是一个标准的解决方案。
因此,我需要帮助的是如何使用 javascript/jquery/etc 发布表单。并在服务器端处理它,返回它并且 å ä ö 看起来像他们应该的那样。如果服务器接收到utf8就完全没问题。原则上是不是应该只使用UTF8就可以解决问题呢?看来应该有另一种解决方案。
This is a question with a lot of information on the net. Still, I haven't solved it.
I am having a dynamically loaded form on a web page. I submit it using jquery $.post. My page is for Swedes so it needs to work with åäö.
When posting data to server, it will then load the data back into a new table. The returned å ä ö is corrupted. Example åååäääööö = åååäääööö
The server side is php. I encode all pages that return content to the browser like this:
<?php header('Content-type: text/html; charset=ISO-8859-1'); ?>
I have tried utf8_encode and utf8_decode on the server side. No difference actually.
And i tried this: encodeURIComponent($(this).serialize()) where $(this) is the form to be submitted. Of course, this doesn't work and I don't know how to encode an object like $(this).
I thought that this is something that a lot of people does, but it doesn't seem to be a standard solution.
Hence, what I need help with is how can I post a form using javascript/jquery/etc. and handle it on the server side, returning it and å ä ö looks as they should. It's totally fine if the server receives utf8. Should the principle be to only use UTF8 and then the problem is solved? Seems like there should be another solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AJAX 使用 UTF-8 编码。如果您的数据不是 UTF-8 格式(例如 Windows 1251 或 ISO-8859-1),您将会遇到麻烦。
在标头中指定不同的编码方案不会将数据转换为 UTF-8。它只是告诉客户期望什么。
我的座右铭:UTF-8 端到端,否则就死!
AJAX uses UTF-8 encoding. If your data is not in UTF-8 format (such as Windows 1251 or ISO-8859-1) you're going to have trouble.
Specifying a different encoding scheme in the header doesn't convert your data to UTF-8. It just tells the client what to expect.
My Motto: UTF-8 end-to-end or die!