国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJS $http模塊POST請求實現

2019-11-19 16:52:52
字體:
來源:轉載
供稿:網友

一、代碼如下:

$http({   method:'post',   url:'post.php',   data:{name:"aaa",id:1,age:20} }).success(function(req){   console.log(req); }) 

解決方案:

1、 

var myApp = angular.module('app',[]);myApp.config(function($httpProvider){ $httpProvider.defaults.transformRequest = function(obj){var str = [];for(var p in obj){str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));}return str.join("&");

  2.  

$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("&");}}).success(function(req){console.log(req);})

 php

 $rawpostdata = file_get_contents("php://input"); $post = json_decode($rawpostdata, true); //傳的數據都在$post中了;

二、 $http請求數據主要會有以下三種方式

1.get請求

2.post請求

3.jsonp

<!DOCTYPE html><html lang="zh_CN"><head>  <meta charset="UTF-8">  <title>Angular基礎</title></head><body><div ng-app="myApp">  <div ng-controller="personCtrl">    姓:<input type="text" ng-model="firstName"/><br/>    名:<input type="text" ng-model="lastName"/><br/>    姓名:<span ng-bind="firstName"></span><span ng-bind="lastName"></span>  </div></div><script src="angular.min.js"></script><script type="application/javascript">  var myApp=angular.module('myApp',[]);  myApp.controller('personCtrl',function($scope,$http){    $http.get('getData.php').        success(function(data) {          console.log(data);        }).        error(function(err) {          //錯誤代碼        });    //$http.post采用postJSON方式發送數據到后臺,    // 解決辦法:在后臺php中使用$postData=file_get_contents("php://input",true);這樣就可以獲得前端傳送過來的數據    var postData={msg:'post的內容'};    var config={params:{id:'5',name:'張三豐'}};    $http.post('postData.php', postData,config).        success(function(data) {          console.log(data);        }).        error(function(err) {          //錯誤代碼        });    var myUrl ="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSON_CALLBACK";    $http.jsonp(myUrl).success(        function(data){          console.log(data);        }    ).error(function(err){          //錯誤代碼        });    $scope.firstName="Wang";    $scope.lastName="Ben";  });</script></body></html>
<?php//postData.php文件//用接收json數據的方式$msg=file_get_contents("php://input",true);$name=$_GET['name'];echo $name.$msg."_post";

顯示效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平原县| 囊谦县| 内丘县| 旬阳县| 慈溪市| 饶平县| 无为县| 安康市| 樟树市| 特克斯县| 乌鲁木齐市| 绥德县| 当阳市| 渭源县| 郓城县| 周至县| 科技| 和平区| 梁山县| 岳西县| 正定县| 呼图壁县| 广水市| 连城县| 建湖县| 阿拉尔市| 石渠县| 红安县| 元谋县| 凌源市| 镇康县| 呼图壁县| 榆林市| 宣武区| 泗阳县| 灌阳县| 延吉市| 吉木乃县| 张家口市| 晋城| 天等县|