Six precautions for Ajax applications

Source: Internet
Author: User

When I came into contact with Ajax, the Ajax support was not very good at that time. It involved the underlying layer and there was no ready-made framework to call for you. List the following common problems.
1. Encoding Problems
Note that the file to be retrieved by Ajax is UTF-8 encoded. After gb2312 encoding is returned to browse, Chinese characters are garbled. If VBScript is used, it can be converted, but VBScript only supports IE.
Therefore, the choice of UTF-8 encoding is a permanent way.

2. Clear the cache of server files.
When a Server File is retrieved using XMLHTTP, the file is cached in the browser. The next time a file is retrieved, the file will not be retrieved from the server. Therefore, you must specify the expiration time of files that are frequently refreshed.

3. When to use XML
X in Ajax refers to XML, but is XML the best solution? Compared with JSON, XML files are larger when the same amount of information is transmitted. Therefore, JSON is a better choice. However, you can also directly output javascript execution, which is more efficient: you do not need to write complex callback functions. But pay more attention to security issues.

4. Two Ajax Methods
Some people may not have a solid foundation and do not know the principles of Ajax. Do you know which two methods are used to implement Ajax?
The oldest one is IFRAME to load a webpage containing Javascript. The script in this webpage uses top. function to call functions on the parent page. However, you can specify the target of a and the target of form, so that you do not need to serialize the form and can easily send the form. Of course, it will be a little troublesome in the future, and it will not be easy to solve problems.
Later, the XMLHTTP component was used. It is also a skill to select a method.

5. Cross-origin problems
If a script is not in the same domain, the two cannot call each other. If the IFRAME is a webpage in another domain, you cannot access the specific content of the parent page. If you need to access remote XML files, such as RSS feed, we recommend that you use the server script for data transfer. It is a good solution when the traffic volume is not very large. However, I read the jquery manual and found that it can call (execute) Cross-origin scripts. It is not clear how to do it. You can download the unencrypted jquery version for research.

6. Security Issues
On the server side, although the client can process many things, you should pay attention to one sentence: all variables from the client are harmful. Without Ajax, many programmers tend to be vigilant. This makes security problems more hidden. If it is an open-source program, the problem will be immediately exposed in front of netizens; in the case of black boxes, it is often difficult to find the cause of intrusion.
Conclusion: In fact, there are not many opportunities to directly write Ajax code in this age of frameworks. Many JS frameworks integrate Ajax call functions and some server-side frameworks, for example, thinkphp also integrates Ajax. However, understanding some underlying things helps to eliminate bugs and improve the performance of Ajax applications.

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.