AJAX-Submitted Coding summary

Source: Internet
Author: User

Today, using the Ajax Post method to submit data to webservice, due to the cause of the email found that the registration method calls always error, open the message a look at the discovery of the submission of the @ symbol has been turned into a%40, [email protected] that is abc%40126. com. At first I thought he had no code. Later Baidu search found. He is urlencode encoded by default.

Why does PHP receive the $_post data in the mailbox symbol @ become%40
It seems that because the data submitted by the form will be UrlEncode, you can use UrlDecode in the PHP background to see if it is consistent.
It was later successfully solved with Httpunility.decode () decoding at the release end.


The difference between Server.URLEncode and httputility.urldecode when encoding a URL, which one should be used? What's the difference between the two?
Test:
String file= "on file (biography). Doc";
String Server_urlencode=server.urlencode (file);
String Server_urldecode=server.urldecode (Server_urlencode);
String Httputility_urlencode=system.web.httputility.urlencode (file);
String Httputility_urldecode=system.web.httputility.urldecode (Httputility_urlencode);
Response.Write ("Original data:" +file);
Sfun.writeline ("Server.URLEncode:" +server_urlencode);
Sfun.writeline ("Server.urldecode:" +server_urldecode);
Sfun.writeline ("Httputility.urlencode:" +httputility_urlencode);
Sfun.writeline ("Httputility.urldecode:" +httputility_urldecode);

Output:
Original data: On file (biography). doc
Server.urlencode:%ce%c4%bc%fe%c9%cf%a3%a8%b4%ab%a3%a9%c6%aa.doc
Server.urldecode: Document (biography). doc
Httputility.urlencode:%e6%96%87%e4%bb%b6%e4%b8%8a%ef%bc%88%e4%bc%a0%ef%bc%89%e7%af%87.doc
Httputility.urldecode: Document (biography). doc

The difference is: Httputility.urlencode () defaults to encoding the URL with UTF8, while Server.URLEncode () encodes the URL with the default encoding.

When developing pages with ASP, we often pass parameters through URLs between pages via System.Web.HttpUtility.UrlEncode and UrlDecode. Paired use of Encode and Decode is no problem.

However, when we write the file download page, we often use the following method to specify the name of the downloaded file:
Response.AddHeader ("Content-disposition", "attachment; Filename="
+ Httputility.urlencode (FileName, Encoding.UTF8));
The reason for converting to UTF8 is to support Chinese file names.

This is the problem, because Httputility.urlencode in the Encode, the space conversion to a plus (' + '), in the Decode when the plus to the space, but the browser is not able to understand the plus is a space, so if the file name contains a space, in the browser When you download the resulting file, the space becomes the plus sign.

One solution is to replace "+" with "%20" (if the original "+" is converted to "%2b") after HttpUtility's urlencode, such as:
filename = httputility.urlencode (filename, Encoding.UTF8);
FileName = Filename.replace ("+", "%20");
Do not understand why Microsoft should convert the space to a plus sign instead of "%20". Remember that the urlencoder of the JDK is to convert the space into "%20".
This is also the case with the. Net 2.0 check.

The above is copied from somewhere else, well written, my own program also encountered the same problem, the default ASPX is encoded in Utf-8, in my program must use GB2312 as the default encoding (<globalization requestencoding= " gb2312 "responseencoding=" gb2312 "/>", the problem arises, Previously no problem of Httputility.urldecode in page.request back value is garbled this is what it says Httputility.urldecode default to UTF8 URL encoding, in which case only Httputility.urldecode You can change it into Server.URLEncode.


When using Ajax to pass data, data is lost when the server side receives data when a plus sign (+), connector (&), or percent (%) is present in the data.

Analyze the format of the Ajax pass-through data and JavaScript syntax:

1. "+" number: javascript resolves to a string connector, so "+" will lose the variable space when the server receives the data.

2. "&": javascript resolves to a variable connector, so when the server side receives data, the data after the & symbol will be lost.

The workaround is to replace the code with regular

Arg.title = Title.replace (/%/g, "%25");
Arg.title = Arg.title.replace (/\&/g, "%26");
Arg.title = Arg.title.replace (/\+/g, "%2b");




Http://zhidao.baidu.com/link?url=zVwXFpKelAHp781C3ZiWbvbFp27fkt7sY91zVw8URboMfXvCYY8gAaL-uxNDa3Bf_nVNWXr_PvZjbRanhdUzLq

Http://www.cnblogs.com/bingyu/articles/1545043.html

AJAX-Submitted Coding summary

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.