Compare three implementations of Ajax and JSON parsing

亚连
Release: 2018-05-24 14:09:50
Original
1420 people have browsed it

This article will introduce you to the three implementations of ajax and related information on json parsing. Friends who are interested in this article can refer to it

This article mainly compares three ways to implement Ajax, and provides a starting point for future learning. .

Preparation:

1、 prototype.js
2、 jquery1.3.2.min.js
3、 json2.js

Background handler (Servlet), access path servlet/testAjax:

Java code

##

package ajax.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Ajax例子后台处理程序 * @author bing * @version 2011-07-07 * */ public class TestAjaxServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String name = request.getParameter("name"); String age = request.getParameter("age"); System.out.println("{\"name\":\"" + name + "\",\"age\":\"" + age + "\"}"); out.print("{\"name\":\"" + name + "\",\"age\":" + age + "}"); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
Copy after login

TestAjaxServlet receives two parameters: name and age, and returns a string written in JSON format.


Front page parameter input interface:

Html code

显示区域

name:
age:

Copy after login

1. Prototype implementation

##Html code

  
Copy after login

In prototype In Ajax implementation, the evalJSON method is used to convert strings into JSON objects.


##2. jquery implementation

Html code

  
Copy after login

I just came into contact with jQuery and used json2.js to process json. I also ask my seniors for advice. .


##3. XMLHttpRequest implementation

Html code

 
Copy after login

ps: Three ways to convert strings into JSON

During project development using Ajax, it is often necessary to convert JSON format into The string is returned to the front end, and the front end parses it into a JS object (JSON).ECMA-262(E3) did not write the JSON concept into the standard, but in ECMA-262(E5) the concept of JSON was officially introduced, including the global JSON object and the Date toJSON method.

1, eval method analysis, I am afraid this is the earliest analysis method.

function strToJson(str){ var json = eval('(' + str + ')'); return json; }
Copy after login

Remember the parentheses on both sides of str.

2, the new Function form is quite weird.

function strToJson(str){ var json = (new Function("return " + str))(); return json; }
Copy after login

In IE6/7, when the string contains a newline (\n), new Function cannot parse it, but eval can.

3, use the global JSON object.

function strToJson(str){ return JSON.parse(str); }
Copy after login

Currently IE8(S)/Firefox3.5/Chrome4/Safari4/Opera10 has implemented this method.

When using JSON.parse, you must strictly abide by the JSON specification. For example, attributes need to be enclosed in quotation marks, as follows

var str = '{name:"jack"}'; var obj = JSON.parse(str); // --> parse error
Copy after login

name is not enclosed in quotation marks Now, when using JSON.parse, exceptions are thrown in all browsers and parsing fails. The first two methods are fine.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.


Related articles:

How to solve the problem that error always pops up when ajax returns verification

Manual solution through Ajax WordPress WP-PostViews does not count the problem

##Analysis and solutions to the reasons why the data obtained by Ajax and put into echarts is not displayed


The above is the detailed content of Compare three implementations of Ajax and JSON parsing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!