700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 怎样使用AngularJS内$http服务Post方法传递json参数

怎样使用AngularJS内$http服务Post方法传递json参数

时间:2019-01-04 20:29:08

相关推荐

怎样使用AngularJS内$http服务Post方法传递json参数

web前端|js教程

javascript,AngularJS,http

web前端-js教程具体如下:

博客源码带会员,ubuntu系统如何上网,跨域tomcat两个项目,unidg爬虫抖音,php变量区分大小写吗,JooYeon_seolzw

A、$http POST方法默认提交数据的类型为application/json

crysadm源码,安装完ubuntu死机,tomcat关闭上传文件,粉笔题库爬虫,php 里面加html,乐云seo哪高粱seolzw

var data = {wid:, praise : 25}; $http.post(url, data).success(function(result) { // });

最终发送的请求是:

android指南针源码,vscode加载插件,ubuntu20.10 美化,tomcat加JAVA,sqlite模糊查询正则,jquery右侧缩略图插件,webpack是前端框架吗,厨房都有那些爬虫,php对象 数组,seo论坛地址,设备管理网站源码,网页右滚动条如何设置,克米设计 dz模板,html5页面左右导航菜单,活动报名管理系统 php源码,vhdl 小程序lzw

POST HTTP/1.1 Content-Type: application/json;charset=utf-8 {wid:,praise:25}

默认的这种方式可以直接将json对象以字符串的形式传递到服务器中,比较适合 RESTful 的接口。但是php脚本的$_POST无法从请求体中获得json数据。

此时可以用:

$data = file_get_contents("php://input"); //获得原始输入流

注:enctype=”multipart/form-data” 的时候 php://input 是无效的

获得请求原始输入流之后再做相应处理就可以获得json数据了。

B、 采用x-www-form-urlencoded 方式提交获得json数据

app.factory("Comment",function($http){ return { get : function(commentFileUrl) { return $http({ method: "GET", url: commentFileUrl, params: {R:Math.random()}, headers: {Cache-Control: o-cache} }); }, //保存一个评论 save : function(toUrl,saveFileUrl,Data) { $http({ method: "POST", url: toUrl, data: {saveUrl:saveFileUrl,commit:Data}, 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(data){ console.log("数据已保存!"); }).error(function(data) { alert("数据保存失败,错误信息:" + JSON.stringify({data:data})); }); } }});var updateClickRate={wid:,click_rate:87};Comment.save("php/updateWork.php","../userdata/work_content.json",JSON.stringify(updateClickRate));

最终发送的请求是:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。