AngularJS中$http的交互問(wèn)題
這篇文章,主要是通過(guò)我們熟悉的jquery中ajax和jsonp的類型方式,總結(jié)一下$http的使用。
$http 是 AngularJS 中的一個(gè)核心服務(wù),用于讀取遠(yuǎn)程服務(wù)器的數(shù)據(jù)。
1. angular中的ajax
寫法一:
$http({
method: 'GET', //可以改成POST
url: '/someUrl'
}).then(function successCallback(response) {
// 請(qǐng)求成功執(zhí)行代碼
}, function errorCallback(response) {
// 請(qǐng)求失敗執(zhí)行代碼
});
示例:
var app = angular.module('myApp', []);
app.controller('siteCtrl', function($scope, $http) {
$http({
method: 'GET',
url: 'https://www.runoob.com/try/angularjs/data/sites.php',
}).then(function successCallback(response) {
console.log(response.data);
}, function errorCallback(response) {
console.log('失敗');
});
});
寫法二:
①GET請(qǐng)求
$http.get('/someUrl',config).then(successCallback, errorCallback);
$http.get('/someUrl',{params:{}}).then(successCallback, errorCallback);
示例:
$http.get({
'http://10.30.24.12/emp-management/empDetail',
{params:{"id":3}}
}).then(function successCallback(response) {
console.log(response.data.name);
}, function errorCallback(response) {
console.log('失敗');
});
②POST請(qǐng)求
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
示例:
$http({
method:'post',
url:'post.php',
data:{name:"aaa",id:"1",age:"20"}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log('失敗');
});
//但是,這時(shí)候你可能收不到返回的數(shù)據(jù),結(jié)果為null,這是因?yàn)橐D(zhuǎn)換成form data。
//解決方案(在post中進(jìn)行相應(yīng)配置):
$http({
method:'post',
url:'post.php',
data:{name:"aaa",id:"1",age:"20"},
headers:{'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log('失敗');
});
/*
原理解讀:
首先,配置headers是因?yàn)?,POST提交時(shí),使用的Content-Type是application/x-www-form-urlencoded,
而使用原生AJAX的POST請(qǐng)求如果不指定請(qǐng)求頭RequestHeader,默認(rèn)使用的Content-Type是text/plain;charset=UTF-8,
在html中form的Content-type默認(rèn)值是Content-type:application/x-www-form-urlencoded,所以要進(jìn)行相應(yīng)的配置。
然后,配置transformRequest是因?yàn)?,如果參?shù)是對(duì)象,需要轉(zhuǎn)化一下。
*/
2.angular中的jsonp
$http({method:'JSONP',url:''}).success().error();
$http.jsonp('/someUrl').success().error();
//這里要注意,跨域請(qǐng)求的url后一定要追加參數(shù)callback,并且callback的值是固定的,即JSON_CALLBACK,盡量不要去做任何改動(dòng)
示例:
$http({
method: 'JSONP',
url: 'http://www.b.com/test.php?callback=JSON_CALLBACK'
}).success(function(response){
console.log(response);
});
$http.jsonp(
'http://www.b.com/test.php?callback=JSON_CALLBACK'
).success(function (response){
console.log(response);
});
3.最后,總結(jié)一下注意事項(xiàng):
(1)代碼里使用的.then()也可以寫成.success().error(),但是v1.5中 $http 的 success 和 error 方法已廢棄,使用 then 方法替代;
(2)關(guān)于參數(shù):用GET的時(shí)候就是params,用POST/PUT/PATCH/DELETE就是data;
(3)$http.jsonp跨域請(qǐng)求的url后一定要追加參數(shù)callback,并且callback的值是固定的,即JSON_CALLBACK,盡量不要去做任何改動(dòng)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS基礎(chǔ) ng-submit 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-submit 指令,這里對(duì)ng-submit 指令的基礎(chǔ)資料做了詳細(xì)介紹整理,并附有代碼示例,有需要的小伙伴可以參考下2016-08-08
AngularJS實(shí)現(xiàn)的自定義過(guò)濾器簡(jiǎn)單示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的自定義過(guò)濾器,結(jié)合實(shí)例形式分析了AngularJS自定義過(guò)濾器的簡(jiǎn)單定義與使用操作技巧,需要的朋友可以參考下2019-02-02
AngularJS路由切換實(shí)現(xiàn)方法分析
這篇文章主要介紹了AngularJS路由切換實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了AngularJS路由切換的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
AngularJS遞歸指令實(shí)現(xiàn)Tree View效果示例
這篇文章主要介紹了AngularJS遞歸指令實(shí)現(xiàn)Tree View效果,結(jié)合實(shí)例形式分析了AngularJS基于遞歸指令實(shí)現(xiàn)樹形結(jié)構(gòu)層次數(shù)據(jù)的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2016-11-11
詳解Angular2學(xué)習(xí)筆記之Html屬性綁定
本篇文章主要介紹了詳解Angular2學(xué)習(xí)筆記之Html屬性綁定,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
解決angular雙向綁定無(wú)效果,ng-model不能正常顯示的問(wèn)題
今天小編就為大家分享一篇解決angular雙向綁定無(wú)效果,ng-model不能正常顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10

