微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angular $http 官方实例说明

General usage

The$httpservice is a function which takes a single argument — aconfiguration object— that is used to generate an HTTP request and returns apromise.

// Simple GET request example: $http({ method: 'GET', url: '/someUrl' }).then(function successCallback(response) { // this callback will be called asynchronously // when the response is available }, function errorCallback(response) { // called asynchronously if an error occurs // or server returns response with an error status. });

The response object has these properties:

  • data{string|Object}– The response body transformed with the transform functions.
  • status{number}– HTTP status code of the response.
  • headersfunction([headerName])}– Header getter function.
  • config{}– The configuration object that was used to generate the request.
  • statusText}– HTTP status text of the response.

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. Also,status codes less than -1 are normalized to zero. -1 usually means the request was aborted,e.g. using aconfig.timeout. Note that if the response is a redirect,XMLHttpRequest will transparently follow it,meaning that the outcome (success or error) will be determined by the final response status code.

Shortcut methods

Shortcut methods are also available. All shortcut methods require passing in the URL,and request data must be passed in for POST/PUT requests. An optional config can be passed as the last argument.

$http.get('/someUrl', config).then(successCallback, errorCallback); $http.post( data, errorCallback);

Complete list of shortcut methods:

原文地址:https://docs.angularjs.org/api/ng/service/$http

原文地址:https://www.jb51.cc/angularjs/149296.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐