Ajax transfer parameters contain special characters for fast resolution _AJAX related

Source: Internet
Author: User

A problem with JQuery Ajax is that there are special characters in the parameters, such as & ' #@, which can cause problems when executing Ajax, because the parameters that are passed are changed. Look at the example and you'll see:

Programme I:

$.ajax ({
  URL: '/ashx/ajax.ashx ',
  type: ' Post ',
  data: ' Option=delete&name=11&adb, Success: function (data) {if (Data!= ' error ') {}}}); '

The Ajax performed above is the asynchronous deletion of a name-11&ABD data when the request to the Ajax.ashx page, we get the name parameter of 11 after the operation will find that the data is deleted with name 11 instead of deleting name as 11& ABC data This is because there are & special characters, the previous two parameters into three parameters option,name,abc then you need to pass the parameter in another way:

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

Using the above JSON format to pass parameters can avoid the problem of parameter errors caused by special characters.

Programme two: Unified coding UTF-8.

1.JSP page:

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

2.ajax.js page: When passing parameters, the parameters of special characters may appear with Escape (encodeURIComponent ()) Two functions for transcoding, passed to the background!

var url = "/zx/servlet/addmemoservlet memo=" + Memocode + "&othermemo=" + Escape (encodeURIComponent (Othermemo)) + "&am" p;applno= "+ applNo.innerText.substr (0, 16); Alert ("Url=" +url); 
Xmlhttp.open ("POST", url, true); 
Xmlhttp.onreadystatechange = Domemo; 

3. The server-side receives the delivered data for example: A servlet's Doget method: Request.setcharacterencoding ("gb2312"); Response.setcontenttype ("Text/xml;charset=utf-8"); Response.setheader ("Cache-control", "No-cache"); ...//The following address the URL passed in Ajax parameter value contains special characters, backend parsing error: To Utf-8 in the way to decode Java.net.URLDecoder urldecoder=new Java.net.URLDecoder (); String Othermemo = Urldecoder.decode (Request.getparameter ("Othermemo"), "Utf-8"); Logger.info ("Othermemo:" + Othermemo);

The above Ajax transfer parameters contain special characters of the fast solution is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.