码迷,mamicode.com
首页 > Web开发 > 详细

Angular——$http

时间:2018-02-06 20:26:30      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:head   formdata   ade   info   快捷方式   lan   传参   请求头   class   

基本介绍

$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

标签:head   formdata   ade   info   快捷方式   lan   传参   请求头   class   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!