Ajax request/response garbled

Source: Internet
Author: User
Ajax request/response garbled

Today, we first encountered a situation where the status management function, which was independently developed by the user, encountered garbled code during partial refresh. We checked the code carefully and found that there was no special issue, charset uses gb18030 in a unified manner, Ajax uses the default UTF-8 to pass Parameters, manually transcode the passed Chinese information. This feature has been in trial for a week, and there is no problem. The strange thing is that today there is a problem ??
  • [Submit Ajax Information]
  • Ajaxrequestobj. createeqstatusparameter = function () {<br/> If (eqstatusarray. length <1) {<br/> return; <br/>}< br/> var paramnamearray = new array (); <br/> var paramvaluearray = new array (); <br/> // retrieves the status array <br/> for (VAR Index = 0, step = eqstatusarray. length; index <step; index ++) {<br/> var tempeqstatusobj = eqstatusarray [Index]; <br/> paramnamearray. push (tempeqstatusobj. eqcode + "'" + tempeqstatusobj. Opdatetime); <br/> paramvaluearray. Push (tempeqstatusobj. status); <br/>}< br/> // splits and generates request strings. <Br/> var paramstr = ""; <br/> paramstr = paramstr. concat ("eqstatusnames = "). concat (encodeuricomponent (paramnamearray. join (","))). concat ("&"); <br/> paramstr = paramstr. concat ("eqstatusvalues = "). concat (encodeuricomponent (paramvaluearray. join ("'"); <br/> return paramstr; <br/> };
    Xmlhttpobj. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ")
    Xmlhttpobj. Send (PARAM );
    The client passes the parameter as a UTF-8 to the application, which can be obtained directly from the request when this parameter is used.
  • [Ajax receives Response Information]
  • Directly use forward to redirect to change the partial content of a div

    If ("doqueryrequest ". equals (actparam) {<br/> equiputils equiputilsobj = equiputils. getinstance (); <br/> // obtain query parameters and splice them <br/> equipstatusmgrobj. setequipcondition (equiputilsobj. createequipfilter (request); <br/> equipstatusmgrobj. setequipstatuscondition (equiputilsobj. getequipstatusdate (request); <br/> // device information <br/> List <dynabean> equiparray = equipstatusmgrobj. queryequip (null); <br/> // device status <br/> Map <string, Map <string, string> equipstatusmapping = <br/> equipstatusmgrobj. queryequipstate4map (equiparray, null); <br/> sessionobj. setattribute ("paginationentity", equipstatusmgrobj. getpaginationentity (); <br/> sessionobj. setattribute ("equipcondition", equipstatusmgrobj. getequipcondition (); <br/> sessionobj. setattribute ("eqstatuscondition", equipstatusmgrobj. getequipstatuscondition (); <br/> request. setattribute ("equiplist", equiparray); <br/> request. setattribute ("datelist", equipstatusmapping); <br/> try {<br/> request. getrequestdispatcher ("/EQ/detectpoint/innereqstatusrefresh. JSP "). forward (request, response); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>} else if ("getpagetoolbar ". equals (actparam) {<br/> paginationentity pentity = (paginationentity) sessionobj. getattribute ("paginationentity"); <br/> string tempstr = pentity. getpaginationtool (); <br/> outputstream out; <br/> try {<br/> response. setcontenttype ("text/html; charsets = UTF-8"); <br/> out = response. getoutputstream (); <br/> out. write (tempstr. getbytes ("UTF-8"); <br/> out. flush (); <br/> out. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/>}
    • Because, currently using xmlhttpobj. responsetext, responsetext is the UTF-8 by default, so it is a good choice to make the jump page into a UTF-8. At least don't worry about garbled characters ..

  • [Cause of garbled characters]
  • Previously, this function was coded in gb18030 format. So far, it was normal for other clients and colleagues' machines, but garbled characters appeared on the pages of my own machines. When I used this function to present the file unified use of UTF-8 OK, I do not know that xmlhttpobj touched the machine's nerves .. I checked some materials and found the international trend. The problem was solved, but the reason was not found!
  • [Found a bug]
  • Response. setcontenttype ("text/html; charset = utf8"); no error is thrown on my machine, but an error is thrown on my colleagues' machine.
    Change Response. setcontenttype ("text/html; charset = UTF-8"); All OK ..

Z

Related Article

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.