繁体   English   中英

角$ http.post()和php?

[英]angular $http.post() and php?

我知道在SO和许多博客上都有几篇文章解释了如何获取php读取的JSON数据。 一些使用url编码,其他使用file_get_contents。

无论如何,它对我不起作用。 我敢肯定有一个非常简单的解释,但是我的代码根本不起作用(请求已发送,后端回答,但该消息或多或少总是相同的:似乎什么都没有到达!

所以在我的控制器中,我有:

var request_data = {
    firstname: 'Quentin',
    lastname: 'Hapsburg'
  };

$http.post("lib/api/public/blog/post", JSON.stringify(request_data))
    .success(function(data) {
        console.log(data);
    })

并在php文件中:

$data = file_get_contents("php://input");
$data = json_decode($data, TRUE);

var_dump($data);

结果为NULL 关于我的错误在哪里的任何建议?

编辑:这可能与重写规则有关,但不是重复的,因为相同的答案不能解决这个问题!

尝试使用application / x-www-form-urlencoded

$http.post(urlBase, $httpParamSerializer(object), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
}).then(function(r) {
    callback(r);
});

然后php将能够使用$ _POST访问您发布的对象

请使用此js文件。 会工作的

Index.html:

<html>
<head>
   <script src="js/angular.min.js"></script>
</head>

<body ng-app="myApp">
<div ng-controller="mainCtrl">
    sdadasdasd
</div>
<script src="app.js"></script>
</body>
</html>

app.js:

var app = angular.module("myApp", []);
app.controller("mainCtrl", function($scope,$http){
    var request_data = {
        firstname: 'Quentin',
        lastname: 'Hapsburg'
    };

    $http.post("test.php", JSON.stringify(request_data)).success(function(data) {
        console.log(data);
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM