method1方法使用的是params参数,该用法会把参数直接附加到url中

method2方法使用的是data参数,该参数会把页面参数类型从默认的multipart/form-data改为application/x-www-form-urlencoded类型,并且将传递的data解析为字符串,该方法会以post参数的方式传递

下面是代码部分:

<html ng-app="myApp">
<head>
<title>angularjs-ajax</title>
<script type="text/javascript" src="../../lib/ionic/js/angular/angular.min.js" charset="utf-8"></script>
</head>
<body ng-controller="ctrl">
<input type="button" value="抓取页面内容1" ng-click="method1()" />
<input type="button" value="抓取页面内容2" ng-click="method2()" />
<div style="border: 1px solid #ccc;width: 500px;height:400px;">{{content}}</div>
<script>
var app = angular.module('myApp',[]); app.config(function($provide){ $provide.factory("transFormFactory",function(){
return {
transForm : function(obj){
var str = [];
for(var p in obj){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
};
});
}); app.controller("ctrl",function($scope,$http,$q,transFormFactory){ $scope.method1 = function() {
$scope.url = "http://localhost:8081/Learning5/T1.action";
$http({method:"POST",url:$scope.url,params:{msg:'abc'}}).success(function (data) {
$scope.content = data;
});
}; $scope.method2 = function() {
$scope.url = "http://localhost:8081/Learning5/T1.action";
$http({method:"POST",url:$scope.url,data:{msg:'123'},transformRequest:transFormFactory.transForm,headers:{'Content-Type': 'application/x-www-form-urlencoded'}}).success(function (data) {
$scope.content = data;
});
};
});
</script>
</body>
</html>

angularjs中ajax请求时传递参数的方法的更多相关文章

  1. 关于angularjs中ajax请求php接口参数个是转换的问题

    mainApp.config(function($httpProvider){ $httpProvider.defaults.transformRequest = function(obj){ var ...

  2. js 中ajax请求时设置 http请求头中的x-requestd-with= ajax

    今天发现 AngularJS 框架的$http服务提供的$http.get() /$http.post()的ajax请求中没有带 x-requested-with字段. 这样的话,后端的php 就无法 ...

  3. spring boot:用cookie保存i18n信息避免每次请求时传递参数(spring boot 2.3.3)

    一,用cookie保存i18n信息的优点? 当开发一个web项目(非api站)时,如果把i18n的选择信息保存到cookie, 则不需要在每次发送请求时都传递所选择语言的参数, 也不需要增加heade ...

  4. JSF拦截ajax请求并传递参数方法

    我们可以利用f:ajax做一些简单的ajax操作,但是遇到复杂的逻辑,它不能简单的去实现,jsf提供了一种方法,可以调用它内部的js方法去实现复杂的逻辑. 首先要在页面引入jsf的js文件: < ...

  5. ASP.NET MVC API与JS进行POST请求时传递参数 -CHPowerljp原创

    在API前添加    [HttpPost] 表示只允许POST方式请求 [HttpPost] public IHttpActionResult Get_BIGDATA([FromBody]Datas  ...

  6. 在 Angularjs 中$state.go 如何传递参数

    在目标页面规定接受的参数: .state('app.AttendanceEditFixed', { url: '/AttendanceEditFixed', params: {'id': null,' ...

  7. Angularjs 跳转页面并传递参数的方法总结

    http://www.zhihu.com/question/33565135 http://www.codeproject.com/Articles/1073780/ASP-NET-MVC-CRUD- ...

  8. jquery中的ajax请求用法以及参数详情

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...

  9. jquery中ajax使用error调试错误的方法

    JQuery使我们在开发Ajax应用程序的时候提高了效率,减少了许多兼容性问题,我们在Ajax项目中,遇到ajax异步获取数据出错怎么办,我们可以通过捕捉error事件来获取出错的信息. jquery ...

随机推荐

  1. 【LeetCode】shell

    195. Tenth Line 输出file.txt中的第十行 答案: # Read from the file file.txt and output the tenth line to stdou ...

  2. openstack多region配置

    实验 A机器 10.64.8.171     RegionOne B机器 10.64.8.142     RegionTwo         Keytson(这个组件随便放在哪台都可以) openst ...

  3. BZOJ1016 JSOI2008最小生成树计数

    定理,在所有最小生成树中,相同边权的边出现的次数相同. 由于重复边权小于10条,可以跑2^10暴力 #include<bits/stdc++.h> using namespace std; ...

  4. [BZOJ4028][HEOI2015]公约数数列(分块)

    先发掘性质: 1.xor和gcd均满足交换律与结合率. 2.前缀gcd最多只有O(log)个. 但并没有什么数据结构能同时利用这两个性质,结合Q=10000,考虑分块. 对每块记录这几个信息: 1.块 ...

  5. 【tarjan+拓扑】BZOJ3887-[Usaco2015 Jan]Grass Cownoisseur

    [题目大意] 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) [思路] 首先 ...

  6. BZOJ 2818: Gcd 筛法

    2818: Gcd 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2818 Description 给定整数N,求1<=x,y< ...

  7. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  8. Transistor latch improves on/off circuitry

    Figure 1 shows an example of on/off circuitry commonly used in battery-operated devices. The p-chann ...

  9. /etc/fstab格式的问题

    [root@localhost etc]# cat fstab /dev/VolGroup00/LogVol00 /                       ext3    defaults    ...

  10. 关于Go语言,自己定义结构体标签的一个妙用.

    在Go中首字母大写和小写,决定着这此变量能否被外部调用, 比如:在使用标准库的json编码自定一的结构的时候: <pre style="margin-top: 0px; margin- ...