Home > Web Front-end > JS Tutorial > body text

How to deal with garbled Chinese characters displayed in the url passed in jquery.ajax

php中世界最好的语言
Release: 2018-04-26 10:51:55
Original
1405 people have browsed it

This time I will show you how to deal with garbled Chinese characters when the url is passed in jquery.ajax. What are the things to note ? The following is a practical case. Let’s take a look. take a look.

JQuery

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

This is the reason why JQuery is garbled. In the future When specifying

character set , ISO-8859-1

ISO8859-1 is used, 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.

And 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 the JQuery code

You only need to simply modify the JQuery code and add charset=UTF-8, so there is no need to change it. If web.config or something changes the encoding in the page, there is no need to use escapc(str) to decode it 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}
Copy after login

2. Js code:

function confirmcommit(){
    var wlCompany = $("#wlCompany").val();//这里含有中文
    var wlId = $("#wlId").val();
    var proposer = $("#proposer").val();
    if(confirm("确认要换货吗")){
$.ajax({
type:'POST',
url:'${pageContext.request.contextPath}/returnGoods/confrimExchangeGoods.do',
data:'wlCompany='+wlCompany+'&wlId='+wlId+'&proposer='+proposer, //直接传值
dataType:'text',
error:function(){
    alert("JQuery AJAX Error!");     
},
success:function(msg){
    alert(msg);
    return;
    if(msg=='换货成功'){
 document.location="${pageContext.request.contextPath}/orderItem/queryProduceItem.do?orderBusType="+${orderBusType};
    }
}
});
     }
 }
Copy after login

3 .Java Code:

public ActionForward confrimExchangeGoods(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
log.info("确认换货 confrimExchangeGoods start...............");
response.setCharacterEncoding("UTF-8"); //这里要设置一下
String wlCompany = request.getParameter("wlCompany");
String wlId = request.getParameter("wlId");
String proposer = request.getParameter("proposer");     .....}
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to deal with page anchor failure in iframe

Detailed explanation of the steps to obtain the document object in iframe

The above is the detailed content of How to deal with garbled Chinese characters displayed in the url passed in jquery.ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template