Home > Web Front-end > JS Tutorial > How to write ajax in jquery

How to write ajax in jquery

(*-*)浩
Release: 2020-09-18 09:47:05
Original
11875 people have browsed it

There are three ways to write AJAX in jQuery, $ajax, $post, and $get. Among them, $post and $get are simple writing methods. When the high-level implementation calls them, it will run the bottom-level encapsulated $ajax.

How to write ajax in jquery

$.ajax writing:

$.ajax({
	type: "POST",
	dataType: "json",
	url: "",
	data: ""
	success: function(data){
		
	},
	error: function(msg){
	
	}
});
Copy after login

Parameters:

1. url:

is required to be a String type parameter, (default is the current page address) the address to which the request is sent.

2. type:

requires parameters of String type, and the request method (post or get) defaults to get. Note that other http request methods such as put and delete can also be used, but are only supported by some browsers.

3. dataType:

Requires parameters of String type, expected data type returned by the server. If not specified, JQuery will automatically return responseXML or responseText based on the http package mime information and pass it as a callback function parameter. The available types are as follows:

  • #xml: Returns an XML document that can be processed with JQuery.

  • html: Returns plain text HTML information; the included script tag will be executed when inserted into the DOM.

  • script: Returns plain text JavaScript code. Results are not automatically cached. Unless cache parameters are set. Note that when making remote requests (not under the same domain), all post requests will be converted into get requests.

  • json: Return JSON data.

  • jsonp: JSONP format. When calling a function using the SONP form, such as myurl?callback=?, JQuery will automatically replace the last "?" with the correct function name to execute the callback function.

  • text: Returns a plain text string.

#data:

  • Requires parameters of type Object or String, data sent to the server.

  • If it is not a string, it will be automatically converted to string format.

  • The get request will be appended to the url. To prevent this automatic conversion, view the processData option.

  • The object must be in key/value format, for example {foo1:"bar1",foo2:"bar2"} is converted to &foo1=bar1&foo2=bar2.

  • If it is an array, JQuery will automatically correspond to the same name for different values. For example, {foo:["bar1","bar2"]} is converted to &foo=bar1&foo=bar2.

#Success:

requires parameters of Function type. The callback function called after the request is successful has two parameters.
(1). Data returned by the server and processed according to the dataType parameter.
(2), a string describing the status.

function(data, textStatus){
	//data可能是xmlDoc、jsonObj、html、text等等
	this;  //调用本次ajax请求时传递的options参数
}
Copy after login

error:

Requires a parameter of Function type, the function to be called when the request fails. This function has three parameters, namely XMLHttpRequest object, error message, and captured error object (optional). The ajax event function is as follows:

function(XMLHttpRequest, textStatus, errorThrown){
  //通常情况下textStatus和errorThrown只有其中一个包含信息
  this;   //调用本次ajax请求时传递的options参数
}
Copy after login

Recommended related articles: ajax video tutorial

The above is the detailed content of How to write ajax in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template