How to use ajax to post form data with non-UTF8 url-encoded string

故事扮演 提交于 2019-12-13 06:08:38

问题


I'm writing a greasemonkey script to add some features to one webpage(so clearly I can't change the code of the server side).

This is a really old website which only receives GBK encoded string. For example, the form in current webpage have a text input field filled up with value "中"("\u4e2d", "%E4%B8%AD" in UTF-8 byte stream, %D6%D0 in GBK byte stream), when the user clicks the submit button, the browser will automatically serialize the form to application/x-www-form-urlencoded text then post it to the server. Because the webpage is claimed to be encoded in GBK, "中" is automatically encoded as "%D6D0%" in the application/x-www-form-urlencoded text, then the server side will decoded it with GBK encoding and get the right result.

Now I'm switching to ajax to submit the form to server(30~40 times most of the time), and I used JQuery's serialize() method to serialize the form to application/x-www-form-urlencoded text, then posted it. The problem is, JQuery's serialize method eventually uses encodeURIComponent to encode all the values of the form, and at this time, "中" is encoded as "%E4%B8%AD" regardless of that the charset of current webpage is GBK. The server side still use GBK to decode it, then failed.

I have googled a lot about this, encodeURIComponent will always encode string as utf-8 stream, and in javascript language there is no utility functions can do what I want, which is get the serialized form data as the browser will automatically do.

Any ideas? thanks.

What about writing an firefox addons and use c/c++ code to implement a XPCOM to do the conversion from javascript string to GBK format url-encoded string?

来源:https://stackoverflow.com/questions/16857295/how-to-use-ajax-to-post-form-data-with-non-utf8-url-encoded-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!