AngularJS $http模块POST请求实现

一、代码如下:

rush:js;"> $http({

method:'post',url:'post.PHP',data:{name:"aaa",id:1,age:20}

}).success(function(req){

console.log(req);

})

解决方案:

1、

rush:js;"> 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.

rush:js;"> $http({

method:'post',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

rush:PHP;"> $rawpostdata = file_get_contents("PHP://input"); $post = json_decode($rawpostdata,true); //传的数据都在$post中了;

二、 $http请求数据主要会有以下三种方式

1.get请求

2.post请求

3.jsonp

rush:js;"> <Meta charset="UTF-8"> Angular基础
irstName"/>
名: 姓名:irstName">
rush:PHP;"> PHP //postData.PHP文件

//用接收json数据的方式
$msg=file_get_contents("PHP://input",true);

$name=$_GET['name'];
echo $name.$msg."_post";

显示效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...