Struts2 + jquery + ajax garbled Problem Solution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.