Example of passing a special string using jQuery ajax

Source: Internet
Author: User

When you use ajax to pass a value to the server, if the value contains special strings such as + and &, the results obtained on the server may be different, these characters are used for other purposes. For example, "+" indicates a connector. After escaping, you get a space.

Let's take a look at the relationship between these special characters and hexadecimal:

+ Space / ? % & = #
% 2B % 20 % 2F % 3F % 25 % 26 & 3D % 23

If the variable really needs to contain these special characters, use the encodeURIComponent () function to filter them. It will encode these characters and the server side will be automatically decoded without processing them.

In addition, try to submit json format data such as {a: "aaa", B: "bbb"} instead of "a = aaa & B = bbb"

Example

The code is as follows: Copy code

<Script type = "text/javascript">
// Alert ("Hello ");
Function good (){
// Alert ("Hello Ajax ");
Var myurl = document. getElementById ("txtUrl"). value;
Var mydata = {"URL": myurl };
Alert (myurl );
Var jsonData = {"ClientName": "Love & Country", "URL": "http: // 192.168.1.8: 9080/GeoSearch? Request = geo & level = geometry & ClientName = US "};
 
$. Ajax ({
Type: "POST ",
// ContentType: "text/xml; UTF-8", // you need to add UTF-8 here
// DataType: "xml ",
Url: "Tserver ",
// DataType: "xml ",
Data: mydata,
// Data: jsonData,
// Data: {"ClientName": "", "URL": "http: // 192.168.1.8: 9080/GeoSearch? Request = geo & level = geometry & ClientName = US "},
// Data: "ClientName = J/& o # Patriotic beauty & # hn @ & location = Boston ",
Success: function (data, textStatus ){
Document. getElementById ("g"). value = data;
// Alert (data );
Alert (jsonData );
},
Error: function (XMLHttpRequest, textStatus, errorThrown ){
// Usually in textStatus and errorThrown
// Only one containing information
// This; // The options parameter passed when calling this AJAX request
// Alert ("bad ");
Alert (textStatus );
// Alert (errorThrown );
}
  
});
   }
</Script>
</Head>
<Body>
<Div id = "url">
Resource address: <input type = "text" size = "50" value = "" id = "txtUrl"/> <br/>
Resource Name: <input type = "text" size = "50" value = "" id = "txtName"/>
</Div>
<Input type = "button" value = "Ajax request" onclick = good ()/>
<Br/>
<TextArea rows = "10" cols = "90" id = "g"> </TextArea>
<Div>
</Div>
      
</Body>
</Html>

Server:

The code is as follows: Copy code

Package test;

Import java. io. IOException;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import java. io .*;

Public class Tserver extends javax. servlet. http. HttpServlet implements javax. servlet. Servlet {
Static final long serialVersionUID = 1L;

Public Tserver (){
Super ();
 }    
 

Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
This. doPost (request, response );
 }   
 

Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Response. setContentType ("text/html ");
// Response. setContentType ("text/xml ");
// Response. setContentType ("application/xml ");
Response. setCharacterEncoding ("UTF-8 ");
Request. setCharacterEncoding ("UTF-8 ");
// Request. setCharacterEncoding ("UTF-8 ");
// String ServerName = request. getParameter ("ClientName ");
String ServerName = request. getParameter ("URL ");
System. out. println (ServerName );
String responseText1 = "your name is:" + ServerName + "for ever from server ";
PrintWriter out = response. getWriter ();
Out. println ("<root> ");
Out. println (responseText1 );
Out. println ("</root> ");
Out. close ();
 }          
}

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.