简体   繁体   English

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

[英]PHP not detecting POST JSON data from JavaScript request

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. 我正在制作一个小型点击游戏,我希望用户能够通过PHP保存和加载数据到我的服务器。 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. 当用户点击我的保存按钮时,它会触发我的vue方法,该方法初始化保存。 I have had no problem getting the data into a JSON format, however I cannot get PHP to read this data via POST. 我将数据转换为JSON格式没有问题,但是我无法让PHP通过POST读取这些数据。 I have checked for network headers, and it shows that stuff is being sent, it seems that PHP just isn't catching it. 我检查了网络标题,它显示正在发送的东西,似乎PHP没有抓住它。 I'll include the code for the JS part and PHP part below. 我将在下面包含JS部分和PHP部分的代码。 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. 如果现在是array_key_exists,那么PHP只会设置为echo,因为在解决了这个问题后,我将很容易处理其余的事情。 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? 我试图遵循这个,到目前为止还没有工作将JSON数据从Javascript发送到PHP?

JS 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

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

Decode the JSON string at the PHP end before accessing values, like this: 在访问值之前,在PHP端解码JSON字符串,如下所示:

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

Update# 1: 更新#1:

As OP commented below, I expect that it will print "SALO Ready" but it is instead doing nothing 正如OP在下面评论的那样, 我希望它会打印出“SALO Ready”,但它却什么都不做

That's because you are not using responseText property of XMLHttpRequest object to see the text received from the server. 那是因为您没有使用XMLHttpRequest对象的responseText属性来查看从服务器收到的文本。 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 参考: https//developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText

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

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