Home Web Front-end JS Tutorial Solution to the problem of Chinese garbled characters being passed in the URL of jquery.ajax_jquery

Solution to the problem of Chinese garbled characters being passed in the URL of jquery.ajax_jquery

May 16, 2016 pm 05:01 PM
ajax jquery Garbled characters

JQuery

JQuery default contentType: application/x-www-form-urlencoded

This is the reason why JQuery is garbled. When the character set is not specified, ISO-8859-1 is used

ISO8859-1, usually called Latin-1. Latin-1 includes additional characters indispensable for writing all Western European languages.

JQuery’s Ajax did not consider the issue of internationalization at all and used the European character set, which caused the problem of garbled characters when transmitting Chinese.

Our UTF-8 can solve this problem.

Ultimately, it means that you need to modify the JQuery code and explicitly declare that the contentType uses the utf-8 character set, which can solve the problem of GB2312 Chinese transmission.

1. Modify JQuery code

You only need to simply modify the JQuery code and add charset=UTF-8. In this way, there is no need to change web.config or change the encoding on the page, and there is no need to use escapc. (str) is then decoded on the server side. How it is conveyed in English is also conveyed in Chinese.

Modify the jquery file used: jquery-1.4.4.min.js

ajaxSettings:{url:location.href,global:true,type:"GET",contentType:"application/x-www-form-urlencoded;charset=UTF-8",processData:true,async:true,xhr:function(){return new E.XMLHttpRequest}

2. Js code:

Copy code The code is as follows:

function confirmcommit(){

var wlCompany = $("#wlCompany").val();//This contains Chinese

var wlId = $("#wlId").val();

var proposer = $("#proposer").val();

if(confirm("Are you sure you want to exchange the product?")){

$.ajax({

type:'POST',

url:'${pageContext.request.contextPath}/returnGoods/confrimExchangeGoods.do',

data:'wlCompany=' wlCompany '&wlId=' wlId '&proposer=' proposer, //pass the value directly

dataType:'text',

error:function(){

alert("JQuery AJAX Error!");

},

success:function(msg){

alert(msg);

return;

if(msg=='Exchange successful'){

document.location="${pageContext.request.contextPath}/orderItem/queryProduceItem.do?orderBusType=" ${orderBusType};

}

}

});

}

}


3. Java code:
Copy code The code is as follows:

public ActionForward confrimExchangeGoods(ActionMapping mapping,

ActionForm form, HttpServletRequest request,

HttpServletResponse response) throws Exception {

log.info("Confirm exchange confrimExchangeGoods start.............");

response.setCharacterEncoding("UTF-8"); //You need to set it here

String wlCompany = request.getParameter("wlCompany");

String wlId = request.getParameter("wlId");

String proposer = request.getParameter("proposer");
.....
}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of garbled characters when importing Chinese data into Oracle? How to solve the problem of garbled characters when importing Chinese data into Oracle? Mar 10, 2024 am 09:54 AM

How to solve the problem of garbled characters when importing Chinese data into Oracle?

How to solve the problem of garbled characters displayed on Win11 when booting? Two solutions to the garbled characters displayed on Win11 boot How to solve the problem of garbled characters displayed on Win11 when booting? Two solutions to the garbled characters displayed on Win11 boot Feb 29, 2024 pm 12:16 PM

How to solve the problem of garbled characters displayed on Win11 when booting? Two solutions to the garbled characters displayed on Win11 boot

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

How to get variables from PHP method using Ajax?

How to deal with garbled characters in Linux terminal How to deal with garbled characters in Linux terminal Mar 20, 2024 pm 03:12 PM

How to deal with garbled characters in Linux terminal

Strategies and techniques for solving Chinese garbled characters in Oracle database Strategies and techniques for solving Chinese garbled characters in Oracle database Mar 08, 2024 am 09:48 AM

Strategies and techniques for solving Chinese garbled characters in Oracle database

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

jQuery Tips: Quickly modify the text of all a tags on the page

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

PHP vs. Ajax: Solutions for creating dynamically loaded content

Detailed explanation of the reasons and solutions for Chinese garbled characters displayed on PHP web pages Detailed explanation of the reasons and solutions for Chinese garbled characters displayed on PHP web pages Mar 26, 2024 pm 12:36 PM

Detailed explanation of the reasons and solutions for Chinese garbled characters displayed on PHP web pages

See all articles