首页 > Web开发 > 详细

Angular——$http

时间:2018-02-06 19:59:41      阅读:255      评论:0      收藏:0      [点我收藏+]

基本介绍

$http用于向服务端发起异步请求,同时还支持多种快捷方式如$http.get()、$http.post()、$http.jsonp。

基本使用

传递的数据可以是‘key=val&key=val‘形式,这种形式叫formData,在请求头设置成   ‘Content-Type‘: ‘application/x-www-form-urlencoded‘  ,那么只有采用这样的方式进行传递

<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul ng-controller="DemoController">
</ul>
<script src="../libs/angular.min.js"></script>
<script>
    var App = angular.module(App, []);
    App.controller(DemoController, [$scope, $http, function ($scope, $http) {
        $http({
            url: 01.php,
            method: post,
            headers: {
                Content-Type: application/x-www-form-urlencoded
            },
            //get
            params: {
                name: itcast,
                sex: 
            },
            //post
            // data: ‘age=10‘
            data: { // post 传参
                age: 10
            }
        }).success(function (info) {
            console.log(info);
        });
    }]);
</script>
</body>
</html>

get方式

<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul ng-controller="DemoController">
</ul>
<script src="../libs/angular.min.js"></script>
<script>
    var App = angular.module(App, []);
    App.controller(DemoController, [$scope, $http, function ($scope, $http) {
        $http({
            url: 02.php,
            method: get,
            params: {
                name: wqx
            }
        }).success(function (info) {
            console.log(info);
        });
    }]);
</script>
</body>
</html>

post

<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul ng-controller="DemoController">
</ul>
<script src="../libs/angular.min.js"></script>
<script>
    var App = angular.module(App, []);
    App.controller(DemoController, [$scope, $http, function ($scope, $http) {
        $http({
            url: 03.php,
            method: post,
            headers: {
                Content-Type: application/x-www-form-urlencoded
            },
            data: age=19
        }).success(function (info) {
            console.log(info);
        });
    }]);
</script>
</body>
</html>

 

Angular——$http

原文:https://www.cnblogs.com/wuqiuxue/p/8423318.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!