简体   繁体   English

接收Json帖子并将其转换为PHP变量

[英]Receive Json post and convert it to PHP variable

i'm trying to convert/retrieve Json Post to php variable. 我正在尝试将Json Post转换/检索为php变量。 i have this payment.php script 我有这个payment.php脚本

<script>
$(document).ready(function(){
    $("#send").click(function(){
        var aksi='';
        var jdata= JSON.stringify({
        "code":"02",
        "accountNo":"5503722012345678",
        "amount":200,
        "transDate":"20190109161439",
        "traceNo":"1234567890",
        "signature":"08CA625868C1E6FFC00D89EA7B668BD7"
        }); 
        $.ajax({
            url:"receive.php",
            type:"POST",
            data:jdata,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
        success:function(resp){
            // Check the values in console
            console.log(resp);
        },
        failure: function(errMsg) {
            alert(errMsg);
        }
        });
    });
});
</script>

and on receive.php i have this code: 在receive.php上,我有以下代码:

//Make sure that it is a POST request.
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
    echo 'Request method must be POST!';
    exit;
}

//Make sure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
    echo 'Content type must be: application/json';
    exit;
}

//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));

//Attempt to decode the incoming RAW post data from JSON.
$decoded = json_decode($content, true);

//If json_decode failed, the JSON is invalid.
if(!is_array($decoded)){
    echo 'Received content contained invalid JSON!';
    exit;
}

//Sample php variable
$code = $decoded['code'];
echo $code;

but i don't get anything on console, whether it error message or the value of $code, it only blank. 但是我在控制台上什么也没得到,无论它是错误消息还是$ code的值,它只是空白。 what are wrong in my code, can you help me? 我的代码有什么问题,您可以帮我吗?

Try with this code, I have removed the datatype from request code. 尝试使用此代码,我已从请求代码中删除了数据类型。

            var aksi='';
            var jdata= JSON.stringify({
            "code":"02",
            "accountNo":"5503722012345678",
            "amount":200,
            "transDate":"20190109161439",
            "traceNo":"1234567890",
            "signature":"08CA625868C1E6FFC00D89EA7B668BD7"
            }); 
            $.ajax({
                url:"http://localhost/stackoverflow.php",
                type:"POST",
                data:jdata,
                contentType: "application/json",    
                success:function(resp){
                    // Check the values in console
                    console.log("kk");
                },
                failure: function(errMsg) {
                    alert(errMsg);
                }
            });

I am also putting the code in script tag please check this also 我也将代码放在脚本标签中,请也检查一下

            <script>
            $(document).ready(function(){
                $("#send").click(function(){
                    var aksi='';
                    var jdata= JSON.stringify({
                    "code":"02",
                    "accountNo":"5503722012345678",
                    "amount":200,
                    "transDate":"20190109161439",
                    "traceNo":"1234567890",
                    "signature":"08CA625868C1E6FFC00D89EA7B668BD7"
                    }); 
                    $.ajax({
                        url:"http://localhost/stackoverflow.php",
                        type:"POST",
                        data:jdata,
                        contentType: "application/json",    
                        success:function(resp){
                            // Check the values in console
                            console.log("kk");
                        },
                        failure: function(errMsg) {
                            alert(errMsg);
                        }
                    });
                });
            });
            </script>

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

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