Quick solution to ajax transmission parameters containing special characters

亚连
Release: 2018-05-24 10:29:25
Original
1650 people have browsed it

Below I will bring you a quick solution to the problem of ajax transmission parameters containing special characters. I hope to be helpful.

JQuery AJAX encounters such a problem. The parameters contain special characters, such as &'#@, etc. At this time, problems will occur when executing AJAX because the parameters passed have changed. Take a look at an example. Just understand:

Option 1:

$.ajax({ url: '/ashx/ajax.ashx', type: 'post', data: 'option=delete&name=11&adb, success: function (data) { if (data != 'error ') { } } }); '
Copy after login

The ajax executed above is to asynchronously delete a data with a name of 11&abd when the request comes When entering the ajax.ashx page, the name parameter we obtained is 11. After executing the operation, we will find that the data with name 11 has actually been deleted, but the data with name 11&abc has not been deleted. This is due to the & special character, which replaces the previous two parameters. It becomes three parameters option, name, abc. At this time, you need to use another method to pass the parameters:

$.ajax({ url: '/ashx/ajax.ashx', type: 'post', data: { 'option': 'delete', 'name': '11&adb' }, success: function(data) { if (data != 'error') {} } });
Copy after login

Use the above json format to pass the parameters. Parameter errors caused by special characters can be avoided.

Option 2: Uniform encoding UTF-8.

1.JSP page:

<%@ page language=" java" pageEncoding="UTF-8"%>

2.Ajax.js page: When passing parameters, parameters with special characters may be transcoded using the escape(encodeURIComponent()) function and passed to Backstage!

var url = "/ZX/servlet/AddMemoServlet memo=" + memoCode + "&otherMemo=" + escape(encodeURIComponent(otherMemo)) + "&applNo=" + applNo.innerText.substr(0, 16); //alert("url="+url); xmlHttp.open("POST", url, true); xmlHttp.onreadystatechange = doMemo; xmlHttp.send(null);
Copy after login

3. The server receives the passed data. For example: in the doGet method of a servlet: request.setCharacterEncoding("gb2312"); response. setContentType("text/xml;charset=utf-8"); response.setHeader("Cache-Control", "no-cache"); ...... //The following solves the parameter value passed by the url in Ajax Contains special characters, back-end parsing errors: decode java.net.URLDecoder in utf-8 format urlDecoder=new java.net.URLDecoder(); String otherMemo = urlDecoder.decode(request.getParameter("otherMemo"), "utf-8"); logger.info("otherMemo:" otherMemo);

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Upload files through Ajax and use FormData to make Ajax requests

##Use ajax to implement asynchronous refresh requests

How to upload files using jQuery Ajax

##

The above is the detailed content of Quick solution to ajax transmission parameters containing special characters. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!