Data acquisition interface provides a summary of two ways

Connect the text:

vue version: https: //www.cnblogs.com/eliwen/p/12047634.html

JQuery version: https: //www.cnblogs.com/eliwen/p/12047614.html

 

1. ajax request sent, through $ .ajax (), the parameter is an object, which has a fixed parameter name.
.ajax $ ({
"url": "the url Data Interface",
"Method": "HTTP request method, and the front end supports only get POST",
"dataType": "Set the server returns the data format, commonly json, html , jsonp, the default value is the json ",
// parameter data to be sent to the back-end, post, the data must be written in the data, get to write the data, you can also follow in the address bar behind the numbers?
" the data ": {
" data name ":" data value ",
}
.}) then (function (RESP) {anonymous function will then automatically call a method when the data is successfully // ajax request
console.log (resp); // data returned from the server
}) .fail (function (error) {// ajax request will automatically fail anonymous function to call a method of data fails
the console.log (error);
});

2. Ajax often used with event / hook operation invocation.

 

jQuery also provides $ .get and $ Operation $ .ajax post of shorthand.

// send a get request
// parameter 1: Request to address the data interface
// Parameter 2: data sent to the address of the interface parameters
// Parameter 3: After the ajax request is successful, the anonymous function call, the first argument anonymous function or data returned from the server
@ 4 parameters: provided the data format returned from the server, to tell the jQuery
$ .get ( "test.php", { "FUNC": "getNameAndTime"},
function (data) {
Alert (data. name); // John
the console.log (data.time); // 2PM
}, "JSON");

// send a post request
// Parameter 1: Data request address interface
@ 2 parameters: the parameter data transmitted to the interface address
@ 3 parameters: ajax request after successful, anonymous function calls, the first argument of the function or anonymous data returned from the server
@ 4 parameters: provided the data format returned from the server, to tell the jQuery
$ .post ( "test.php", { "FUNC": "getNameAndTime"},
function (data) {
Alert (data. name); // John
the console.log (data.time); // 2PM
}, "JSON");

Guess you like

Origin www.cnblogs.com/eliwen/p/12047644.html