发表于: 2017-05-28 20:15:46

1 823


今天完成的任务:

对angular ajax 方法进行了学习,并配合demo进行深入的理解。


明天的计划:

准备复盘项目相关事宜,学习复盘方面的知识。


遇到的问题:

1 angular ajax方法 的总结:

$http 服务:它接受一个设置对象,其中指定了如何创建 HTTP 请求;返回一个 promise 对象,其中提供两个方法: success 方法和 error方法。

标准的使用方法:

$scope.uploadNow = function () {
$scope.statusValue = 2;
   $http({
method: 'post',
       url: '/carrots-admin-ajax/a/u/article?id='+$scope.id,
       headers: {'Content-Type': 'application/x-www-form-urlencoded'},
       data: $.param({
title: $scope.editTitle,
           type: $scope.selectType,
       })
}).then(function (res) {
if (res.data.code === 0) {
bootbox.alert('编辑上线成功');
           $state.go('main.article');
       } else {
bootbox.alert(res.data.message);
       }
});
};

其中,$http 接受的配置项有:

method 方法

url 路径

params GET请求的参数

data post请求的参数

headers 头,这个请求头在使用时要特别注意

transformRequest 请求预处理函数

transformResponse 响应预处理函数

cache 缓存

timeout 超时毫秒,超时的请求会被取消

withCredentials 跨域安全策略的一个东西

其中的 transformRequest 和 transformResponse 及 headers 已经有定义的,如果自定义则会覆盖默认定义

对于几个标准的 HTTP 方法,有对应的 shortcut :

$http.delete(url, config)

$http.get(url, config)

$http.head(url, config)

$http.jsonp(url, config)

$http.post(url, data, config)

$http.put(url, data, config)

注意其中的 JSONP 方法,在实现上会在页面中添加一个 script 标签,然后放出一个 GET 请求。你自己定义的,匿名回调函数,会被 ng 自已给一个全局变量。在定义请求,作为 GET 参数,你可以使用 JSON_CALLBACK 这个字符串来暂时代替回调函数名,之后 ng 会为你替换成真正的函数名:

var p = $http({
method: 'JSONP',
   url: '/json',
   params: {callback: 'JSON_CALLBACK'}
});
p.success(function(response, status, headers, config){
console.log(response);
   $scope.name = response.name;
});

$http 有两个属性:

defaults 请求的全局配置

pendingRequests 当前的请求队列状态

$http.defaults.transformRequest = function(data){

    console.log('here'); return data;}

console.log($http.pendingRequests);


收获:

以上



返回列表 返回列表
评论

    分享到