簡體   English   中英

將帶有角度的 $http post 的對象發送到 PHP 數組 key=>value

[英]Send object with $http post in angular to a PHP array key=>value

我正在嘗試通過 POST 將對象發送到 PHP 並將其轉換為關聯數組

    postData: function(word, description, translate) {
        var formData = {
            w: word, 
            d: description, 
            t: trasnlate
        };

        $http({ 
            method: 'POST', 
            url: 'db.php', 
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: formData
        }).
            success(function(data, status, headers, config) {
                console.log(data);
            }).
            error(function(data, status, headers, config) {

            });
    }

數據庫文件

print_r($_POST); 

控制台日志(數據):

 Array(
[{"w":"word","d":"description","h":"translate"}] => )

我想要這樣的東西:

 Array(
[w] => word
[d] => description
[t] => translate

)

在您的 PHP 代碼中執行以下操作:

$json = file_get_contents('php://input');
$array = json_decode($json, true);

如果 POST 有效負載中提供了適當的 JSON 對象,則$array將是一個關聯數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM