douyan6871 2019-03-19 22:07
浏览 136
已采纳

Fetch不发送帖子数据[重复]

This question already has an answer here:

I'm having this problem, where for some reason I can't send data to another PHP script with POST over the fetch API.

I've already checked other solutions and cleaned up my code, but it still won't work.

Maybe I'm missing something.

Here's my code:

function request(where, num, last, query){
    for (var i = num; i > 0; i--) {
        fetch("global/modul.php", {
            method: "POST",
            body: JSON.stringify({
                last: last,
                query: query
            }),
            headers:{
                'Content-Type': 'application/json'
            }
        }).then(function (response) {
            return response.text();
        }).then(function (data) {
            let html = data.trim();
            document.getElementById(where).innerHTML += html.trim();
        }).catch(function (error) {
            console.log('Request failed', error);
        });

        last = document.getElementById(where).lastChild.id;
    }
}

    var_dump($_POST);

Prints:

array(0) { }

I've also tested the solutions shown in Parse Javascript fetch in PHP, but it didn't solve the problem. I still don't get the POST data in my php-script.

</div>
  • 写回答

1条回答 默认 最新

  • duanci9305 2019-03-19 22:24
    关注

    There is no problem with your JS code. However you need to look for the data in another variable in your PHP code, by reading php://input like so:

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

    Source: https://stackoverflow.com/a/39508364/2831645

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

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?