A quick solution for ajax Transfer Parameters containing special characters.

Source: Internet
Author: User

A quick solution for ajax Transfer Parameters containing special characters.

JQuery AJAX encounters such a problem that the parameter contains special characters, such as & '# @. In this case, a problem occurs when AJAX is executed, because the parameter passed has changed. take a look at the example to understand:

Solution 1:

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

The ajax executed above is to asynchronously delete a data with name 11 & abd when the request is sent to ajax. on the ashx page, when we get the name parameter "11", we will find that the data whose name is 11 has been deleted, the data whose name is 11 & abc is not deleted. This is because there are & special characters, the previous two parameters are changed to three parameters: option, name, in this case, you need to use another method to pass the parameter:

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

Passing parameters in json format can avoid parameter errors caused by special characters.

Solution 2: Unified coding 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 the background!

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); 

3. the data received by the server, 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 solution solves the problem of backend parsing errors when url-passed parameter values contain special characters in Ajax: Decoding java.net using UTF-8. URLDecoder urlDecoder = new java.net. URLDecoder (); String otherMemo = urlDecoder. decode (request. getParameter ("otherMemo"), "UTF-8"); logger.info ("otherMemo:" + otherMemo );

The quick solution for the above ajax transmission parameters containing special characters is to share all the content with you in the editor. I hope you can give us a reference, and hope you can also support the help house.

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.