angularjs中ajax请求时传递参数的方法
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请求时传递参数的方法的更多相关文章
- 关于angularjs中ajax请求php接口参数个是转换的问题
mainApp.config(function($httpProvider){ $httpProvider.defaults.transformRequest = function(obj){ var ...
- js 中ajax请求时设置 http请求头中的x-requestd-with= ajax
今天发现 AngularJS 框架的$http服务提供的$http.get() /$http.post()的ajax请求中没有带 x-requested-with字段. 这样的话,后端的php 就无法 ...
- spring boot:用cookie保存i18n信息避免每次请求时传递参数(spring boot 2.3.3)
一,用cookie保存i18n信息的优点? 当开发一个web项目(非api站)时,如果把i18n的选择信息保存到cookie, 则不需要在每次发送请求时都传递所选择语言的参数, 也不需要增加heade ...
- JSF拦截ajax请求并传递参数方法
我们可以利用f:ajax做一些简单的ajax操作,但是遇到复杂的逻辑,它不能简单的去实现,jsf提供了一种方法,可以调用它内部的js方法去实现复杂的逻辑. 首先要在页面引入jsf的js文件: < ...
- ASP.NET MVC API与JS进行POST请求时传递参数 -CHPowerljp原创
在API前添加 [HttpPost] 表示只允许POST方式请求 [HttpPost] public IHttpActionResult Get_BIGDATA([FromBody]Datas ...
- 在 Angularjs 中$state.go 如何传递参数
在目标页面规定接受的参数: .state('app.AttendanceEditFixed', { url: '/AttendanceEditFixed', params: {'id': null,' ...
- Angularjs 跳转页面并传递参数的方法总结
http://www.zhihu.com/question/33565135 http://www.codeproject.com/Articles/1073780/ASP-NET-MVC-CRUD- ...
- jquery中的ajax请求用法以及参数详情
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- jquery中ajax使用error调试错误的方法
JQuery使我们在开发Ajax应用程序的时候提高了效率,减少了许多兼容性问题,我们在Ajax项目中,遇到ajax异步获取数据出错怎么办,我们可以通过捕捉error事件来获取出错的信息. jquery ...
随机推荐
- git实现github仓库和本地仓库同步
配置git 安装git以后,打开git bash,首先要对git进行配置,输入 git config --global username "你的名字" git config --g ...
- 【BZOJ 3747】 3747: [POI2015]Kinoman (线段树)
3747: [POI2015]Kinoman Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 830 Solved: 338 Description ...
- 一个安卓应用 多少个 dalvik 虚拟机
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 一个
- [CC-ANUCBC]Cards, bags and coins
[CC-ANUCBC]Cards, bags and coins 题目大意: 给你\(n(n\le10^5)\)个数,\(q(q\le30)\)次询问,问从中选取若干个数使得这些数之和为\(m(m\l ...
- vmware10上三台虚拟机的Hadoop2.5.1集群搭建
由于官方版本的Hadoop是32位,若在64位Linux上安装,则必须先重新在64位环境下编译Hadoop源代码.本环境采用编译后的hadoop2.5.1 . 安装参考博客: 1 http://www ...
- hdu 1171 多重背包
题意:给出价值和数量,求能分开的最近的两个总价值,例如10,20*2,30,分开就是40,40 链接:点我 #include<cstdio> #include<iostream> ...
- 【BZOJ】2724: [Violet 6]蒲公英
2724: [Violet 6]蒲公英 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2900 Solved: 1031[Submit][Statu ...
- jquery加载解析XML文件
xml文件 <?xml version="1.0" encoding="utf-8" ?> <taxrates> <taxrate ...
- hdu 5224 Tom and paper 水题
Tom and paper Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/6 ...
- pytest文档4-测试用例setup和teardown
前言 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次. 当然还有更高级一点的setupClass和teardownClass,需 ...