dtcyv3985 2019-04-14 18:10
浏览 79
已采纳

PHP没有从JavaScript请求中检测POST JSON数据

I am in need of a little help with a small bit of my code that is essential to my application. I am making a small clicker game, and I want users to be able to save and load data via PHP to my server. I do not want to use Local Storage to make it harder for anyone to edit their economy and "cheat". When the user clicks on a save button I have, it fires my vue method which initializes the saving. I have had no problem getting the data into a JSON format, however I cannot get PHP to read this data via POST. I have checked for network headers, and it shows that stuff is being sent, it seems that PHP just isn't catching it. I'll include the code for the JS part and PHP part below. The PHP is only set to echo if the array_key_exists right now, as after getting this sorted out I will easily be able to handle the rest. Any help would be greatly appreciated!

I have tried to follow this, which has not worked so far Send JSON data from Javascript to PHP?

JS

saloOut: function() {
            var saveData = {
                saveMoney: this.money,
                saveCrystals: this.crystals,
            };
            saveData = "saveData=" + (JSON.stringify(saveData));
            var sendData = new XMLHttpRequest();
            sendData.open("POST", "salo.php", true);
            sendData.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            sendData.send(saveData);
            console.log(saveData);
        }

PHP

<?php
    if (array_key_exists("saveData", $_POST)) {
        echo "<p>SALO Ready!</p>";
    }
?>
  • 写回答

1条回答 默认 最新

  • dongshan7708 2019-04-14 18:24
    关注

    Decode the JSON string at the PHP end before accessing values, like this:

    <?php
        if(isset($_POST['saveData'])){
            $result = json_decode($_POST['saveData'], true);
            // use $result['saveMoney'] and $result['saveCrystals'] accordingly
        }
    ?>
    

    Update# 1:

    As OP commented below, I expect that it will print "SALO Ready" but it is instead doing nothing

    That's because you are not using responseText property of XMLHttpRequest object to see the text received from the server. Use below snippet to see the response text.

    saloOut: function() {
        var saveData = {
            saveMoney: this.money,
            saveCrystals: this.crystals,
        };
        saveData = "saveData=" + (JSON.stringify(saveData));
        var sendData = new XMLHttpRequest();
        sendData.open("POST", "salo.php", true);
        sendData.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        sendData.send(saveData);
    
        sendData.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                alert(this.responseText);
            }
        };
    }
    

    Reference: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况