JQuery_review implements asynchronous loading through the $. get () and $. post () methods. jquery_review.post

Source: Internet
Author: User

JQuery_review implements asynchronous loading through the $. get () and $. post () methods. jquery_review.post
$. Get () and $. post () are two methods. As the name suggests, one is to get data through the get method, and the other is to get data through the post method. What are the differences between the two methods? There are three key differences: the first get transfer data theory is within 2 kb, and the post method is not restricted in principle. In the second aspect, the current request content is displayed in the address bar, which is not good when there is a user name and password. The other is in the Request body. Although this is not very secure, it is at least a little safer than the GET method. Third, in fact, the get method was initially used to design the request for static content, while the POST method was used to submit the addition, deletion, and modification data, but the distinction later was not so obvious.
Let's look back at the $. get () and $. post methods. Both methods can accept four parameters. The first parameter is to tell jQuery where I want to request data. The second parameter is an optional parameter, it is used to tell jQuery what the request data is, and the request data will become part of the request data. It is mainly used to pass the key/value to the server for processing. The third parameter is the callback parameter, which is called only when the call is successful. There is a problem here. What should I do if the callback fails? At this time, you should refer to the jQuery global method for handling returned exceptions. The last parameter is the data type that the server expects to pass back. The data types include but are not limited to xml, html, json, script, and so on. In particular, xml, which is actually the same as html, is probably the same as their ancestor. Therefore, many jquery methods originally used in html can be used in xml. For example, the $ () Universal factory can be used to convert the returned xml data into an object. find () to find the child element, use attr to find the attribute value, and use. text () to find the text value in the element.
Another thing to note is that the front-end writes the data to be sent from the backend in xml format, so the backend should send the data according to the front-end Conventions: response. setContentType ("text/xml; charset = UTF-8 ");

The Server code is as follows:

@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>");StringBuffer xmlSb = new StringBuffer();xmlSb.append("<book>") .append("<title>") .append("No ventured No gained") .append("</title>") .append("<publish>") .append("Chinses publish") .append("</publish>") .append("</book>");//resp.setContentType("text/html;charset=utf-8");resp.setContentType("text/xml;charset=utf-8");resp.getWriter().print(xmlSb.toString());}


The front-end code is as follows:

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




When the jquery post method is used for asynchronous access, the returned json data cannot be parsed?

1. If you specify datatype: 'json', it will not work if the background is not converted to json format.
2. parse is used to parse json objects from a string.
3. If the background returns an object in json format, you can directly access the object through the data point attribute.
If the backend is list to josn, you can use the data [0] Point attribute. Of course, your ype is json.

Java and JQuery problems, $ ajax (), $ post (), and $ get (). Under what circumstances do you use them? What are their differences?

Full parsing of jQuery Ajax instances

JQuery is indeed a very lightweight JS framework that can help us quickly develop JS applications and change the habit of writing JavaScript code to a certain extent.
Let's talk nonsense and go straight to the question. Let's take a look at some simple methods, all of which are for jQuery. ajax () is encapsulated to facilitate our use of the method. Of course, if you want to process complicated logic, you still need to use jQuery. ajax () (this will be mentioned later ).
1. load (url, [data], [callback]): load the remote HTML file code and insert it into the DOM.
Url (String): the URL of the requested HTML page.
Data (Map): (optional) key/value data sent to the server.
Callback (Callback): (optional) callback function when the request is completed (success is not required.
By default, this method uses the GET method to pass data. If the [data] parameter passes data in, it is automatically converted to the POST method. In jQuery 1.2, you can specify a selector to filter loaded HTML documents. In DOM, only filtered HTML code is inserted. The syntax is like "url # some> selector ".
This method can easily dynamically load HTML files, such as forms.
Sample Code:
$ (". Ajax. load"). load ("www.cnblogs.com/...2.html. post ",

Function (responseText, textStatus, XMLHttpRequest ){

This; // here this points to the current DOM object, that is, $ (". ajax. load") [0]

// Alert (responseText); // content returned by the request
// Alert (textStatus); // Request status: success, error
// Alert (XMLHttpRequest); // XMLHttpRequest object
});

The result is displayed here.

Note: I don't know why an error occurs when the absolute URL write path is in FF. please let us know. The following get () and post () Examples use absolute paths, so you will encounter errors in FF and will not see the returned results. The get () and post () Examples are all cross-origin calls, and the results cannot be obtained after being uploaded. Therefore, the run button is removed.

2. jQuery. get (url, [data], [callback]): Use the GET Method for asynchronous requests.
Parameters:
Url (String): the URL of the request.
Data (Map): (optional) data to be sent to the server, which is expressed in Key/value pairs and will be appended to the request URL as QueryString.
Callback (Function): (optional) callback Function when the load is successful (this method is called only when the Response returns success ).
This is a simple GET request function to replace complex $. ajax. When the request is successful, you can call the callback function... the remaining full text>

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.