JQuery_review: asynchronously loads HTML documents using the. load () method,

Source: Internet
Author: User

JQuery_review: asynchronously loads HTML documents using the. load () method,
Native ajax compilation is quite troublesome. We should not only handle different initialization of XMLHttpRequest objects in different browsers, but also remember the attributes and methods of a large number of XMLHttpRequest objects, in addition, much of the work is repetitive, just as the implementation method of jdbc is the same. JDBC has many fixed modes, therefore, both Hibernate, Ibatis, and Spring use the template mode to encapsulate a large number of identical calls, making the entire method easier to use. For programmers, these are some very good things, because there is no need to duplicate the wheel, duplicate the wheel, high costs, performance may not be proportional to the cost, so as a programmer, we should always clarify the choice of self-control and procurement, as the boss said: "Our technical staff should not worship technology in a religious manner, and only the sold technologies will have value", therefore, it makes sense that HW is only a little ahead in technology. The former Bell lab never saw smoke and the cloud.
JQuery encapsulates ajax very well. For example, we have a requirement, the requirement is to obtain an Html code from the server <div> <span> stringValue </span> </div>, but we only want the span element, and add the span element to the local HTML. The load method is not suitable. The load () method has three parameters to choose from. The first parameter is the url, which is used to tell jquery where to request the html document. The second parameter is a key-value pair. Generally, jQuery's key-value pairs support {key: value, key: value. The third is the callback function. no matter whether the execution is successful or not, the callback function will always be executed.

In particular, the first parameter of the load method, a space can be added after the url, and then the filtered content can be written. For example, the above can be written as "url span", it tells jQuery that after we get the returned html document, we only need the span element. This is a very practical method. For the second parameter of load, if it is null, jQuery uses the get method to send ajax messages intelligently. If it contains parameters, then the system automatically forwards the parameters using POST. (TOBO verification, if the url is followed? Name = value to pass the function. Will post or get be used ?). The third parameter is the callback function. In fact, the callback function is very useful in actual development, because we cannot guarantee that all ajax requests can be correctly matched, for this, you can refer to jQuery's official documentation and have a clear understanding of the default three parameters of the callback function. This is very important.

The DEMO code of the server is as follows:

package com.samsung.servlet;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String name = req.getParameter("name");String address = req.getParameter("address");StringBuffer sb = new StringBuffer();sb.append("<div><span>hi! nice to meet you! ");sb.append(name).append(",(");sb.append(address).append("),");sb.append("how are you getting along?</span></div>");resp.getWriter().print(sb.toString());}}


The front-end DEMO code is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">






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.