内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何解決AngularJS中$http的交互問題

這篇文章主要為大家展示了“如何解決AngularJS中$http的交互問題”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“如何解決AngularJS中$http的交互問題”這篇文章吧。

創(chuàng)新互聯(lián)是一家專業(yè)提供婁星企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為婁星眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計公司優(yōu)惠進行中。

這篇文章,主要是通過我們熟悉的jquery中ajax和jsonp的類型方式,總結(jié)一下$http的使用。

$http 是 AngularJS 中的一個核心服務(wù),用于讀取遠程服務(wù)器的數(shù)據(jù)。

1. angular中的ajax

寫法一:

$http({
  method: 'GET', //可以改成POST
  url: '/someUrl'
}).then(function successCallback(response) {
    // 請求成功執(zhí)行代碼
  }, function errorCallback(response) {
    // 請求失敗執(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請求

$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請求

$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ù)據(jù),結(jié)果為null,這是因為要轉(zhuǎn)換成form data。 
//解決方案(在post中進行相應(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是因為,POST提交時,使用的Content-Type是application/x-www-form-urlencoded,
而使用原生AJAX的POST請求如果不指定請求頭RequestHeader,默認使用的Content-Type是text/plain;charset=UTF-8,
在html中form的Content-type默認值是Content-type:application/x-www-form-urlencoded,所以要進行相應(yīng)的配置。
然后,配置transformRequest是因為,如果參數(shù)是對象,需要轉(zhuǎn)化一下。
*/

2.angular中的jsonp

$http({method:'JSONP',url:''}).success().error();
$http.jsonp('/someUrl').success().error();

//這里要注意,跨域請求的url后一定要追加參數(shù)callback,并且callback的值是固定的,即JSON_CALLBACK,盡量不要去做任何改動

示例:

$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é)一下注意事項:

(1)代碼里使用的.then()也可以寫成.success().error(),但是v1.5中 $http 的 success 和 error 方法已廢棄,使用 then 方法替代;

(2)關(guān)于參數(shù):用GET的時候就是params,用POST/PUT/PATCH/DELETE就是data;

(3)$http.jsonp跨域請求的url后一定要追加參數(shù)callback,并且callback的值是固定的,即JSON_CALLBACK,盡量不要去做任何改動。

以上是“如何解決AngularJS中$http的交互問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享題目:如何解決AngularJS中$http的交互問題
網(wǎng)址分享:http://www.rwnh.cn/article42/gshdhc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、定制網(wǎng)站網(wǎng)站導航、營銷型網(wǎng)站建設(shè)商城網(wǎng)站、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司
城口县| 木里| 浦江县| 宜阳县| 公安县| 怀柔区| 安福县| 商丘市| 江陵县| 合江县| 新疆| 长汀县| 韶关市| 锦州市| 铜鼓县| 永嘉县| 大荔县| 沐川县| 株洲市| 确山县| 长阳| 开远市| 娱乐| 和硕县| 麦盖提县| 黄大仙区| 法库县| 怀宁县| 平度市| 保山市| 东莞市| 岐山县| 准格尔旗| 偃师市| 肇东市| 噶尔县| 鹤岗市| 共和县| 灵寿县| 涡阳县| 台北县|