Request for ajax for $http for Angular (Recommended)
- 2021-07-10 18:13:35
- OfStack
angular encapsulates the ajax request by encapsulating the $http method
Also encapsulated by $http
$http.get()
And
$http.post()
Here, because the diagram saves trouble, these two methods are not as good as using the general method directly
Here first write a case, first write an object
var req = {
method: 'POST',// Mode of request
url: ip,// Requested address
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': '*/*'
},// The header of the request, if it can not be written by default
timeout:5000,// Timeout, not yet tested
data: str //message Must be a=b&c=d Format of
};
Then call the object through the $http method to get the information
$http(req).success(function (data, status, headers, config) {
// Data processing after success
}).error(function (data, status, headers, config) {
// Prompt after failure
console.log("error", data, status, headers, JSON.stringify(config));
});
angular's ajax uses the way of chain calling, which is convenient and easy to understand
Configuration Items for $http
method: Request mode, including GET/DELETE/HEAD/JSONP/POST/PUT
url: Absolute or Relative Request Target
data or params: Requested data transfer, data is the data requested by post, params is the data requested by get
headers: Header of request
timeout: Set the request timeout time, in milliseconds, 1000 equals 1 second