Struts2 + jquery + ajax garbled Problem
At the beginning, I learned javaee and used struts2 + dbutils + ajax + jquery. If I want to input a user name on the login page, if the database does not exist, I will use alert to give a prompt, garbled characters are found. My page is gbk and I don't want to change it. struts2 is also configured with gbk, because the UTF-8 encoding used for passing parameters by ajax by default is solved as follows:
1. Modify the js Code:
Var url = "validatename. action ";
Var m_username = encodeURIComponent ($ ("# username"). attr ("value "));
Var params = {username: m_username}; jQuery. post (url, params, callbackFun, "json ");
2. Modify validatename. action and convert the username encoding passed through ajax.
This. username = java.net. URLDecoder. decode (username, "UTF-8 ");
The preceding two points must be modified at the same time.
In addition, I think there should be a better way to use the struts2 Interceptor to write an interceptor to intercept all ajax requests. That would be better and more common, if you have any experts, refer to that practice.
Source Address
Http://www.blogjava.net/sdcharles/articles/321840.html