簡體   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