JSONP属于AJAX吗?ajax是指通过使用xmlhttprequest对象进行异步数据交互的技术,jsonp是依靠scriptsrc属性来获取的,不属于ajax
JSONP有什么缺点,使用的时候需要注意什么?
不能post跨域处理,需要注意的是:每次请求应该动态的创建script标签和回调函数,数据获取完成后销毁。
如果method是jsonp的话,就可以用jsonp去跨域请求,但是注意要在url后写关于callback的值为JSON_CALLBACK
百度搜索小例子
这里引用的是 angular-sanitize.js
var app = angular.module("myapp",['ngSanitize'])
app.controller("myController",function ($scope,$http) {
$http({ url:"http://datainfo.duapp.com/shopdata/getclass.php",
method:"post",
params:{a:1}
}).success(function (results) {
$scope.dataList = results
}).error(function (error) {
console.log(error)
})
})
app.controller("yourController",function ($scope,$http) {
$scope.search = function () {
$http({ url:"https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",
method:"jsonp",
params:{
wd:$scope.wd,
cb:'JSON_CALLBACK'
}
}).success(function (results) {
$scope.dataList = results.s
})
}
})
$location服务
console.log($location.absUrl())//输出绝对地址
console.log($location.host())//输出域名
console.log($location.port())//输出端口
console.log($location.protocol())//协议
$location.path("aaa")//在路由中控制切换页面
console.log($location.path()) // #/aaa
$log 服务
多种控制台输出模式
$log.info("info");
$log.warn("warn");
$log.error("error");
$log.log("log");
angularJs对服务供应商配置
例如
myapp.config(["$interpolateProvider",function($interpolateProvider){
$interpolateProvider.startSymbol("!!");
$interpolateProvider.endSymbol("!!");
}])
angular就不认识{{}}了,开始变成!!!!
自定义服务 三种
1.factory
myapp.factory('serviceName',function(){
return ....
})
可以return 字符串、数组、函数、对象(使用最多,最和逻辑)
引入方法和angualr自带的前面加$的服务完全一样,使用方法取决于return出来的是什么东西,自定义服务的服务名还是别加$了
eq:返回一个 两个数之间的随机数的服务
myapp.factory("myService",function(){
return {
getRandom:function(a,b){
return Math.random()*(b-a)+a;
}
}
})
自定义的服务可以依赖注入其他服务
