angularJs的那些坑——$http服务

来源:互联网 发布:mac 下html开发工具 编辑:程序博客网 时间:2024/06/05 06:06

最近一段时间在学习angularJs,测试的时候发现http服务post传的数据,后台接收不到。
js:
angular.module("http",[]).factory("httpService",["$http",function($http){
var c = {};
return c.login = function(a){
var url = "login.php";
return $http({
method : "POST",
url:url,
data:$.param(a)
})
},c
}])

login.php

if($_POST){    $username = $_POST['email'];    $password = $_POST["password"];    if(($username == $admin['username']) && ($password == $admin['password'])){        echo '{"code":"200","list":null,"msg":"登录成功"}';    }}

查资料才知道,angular http服务默认的Content-Type为application/json,而php的$_POST只能接收Content-Type为application/x-www-form-urlencoded的数据。所以需要把http服务加一个参数
return $http({
method : "POST",
url:url,
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
},
data:$.param(a)
})

另外php的输入流php://input可以获取到Content-Type为application/json的数据。$data = file_get_contents("php://input");且php://input不能用于enctype=multipart/form-data”(这个应该是表单提交的数据)。

0 0
原创粉丝点击