简体   繁体   English

为什么使用 post 获取 .php 文件返回空白结果

[英]Why does fetching a .php file using post return a blank result

Okay, running this in a Node.JS enviornment (using node-fetch) works just fine, it returns the expected result.好的,在 Node.JS 环境中运行它(使用 node-fetch)工作得很好,它返回预期的结果。

fetch(`<URL>/api.php?f=developmentIsAnnoying2`,{method:"post",body:JSON.stringify({time:0,queue:["a song"],
repeat:false,loop:false})}).then(d=>d.text()).then(console.log);

But running the same code in Google Chrome, using the same domain, returns an absolutely blank result.但是在谷歌浏览器中运行相同的代码,使用相同的域,返回一个绝对空白的结果。 There are no errors, on any side (client, php, nor API).任何方面(客户端、php 或 API)都没有错误。

PHP: PHP:

    header("Content-Type: application/json");
    switch($_SERVER['REQUEST_METHOD']) {
        case "POST":
            echo file_get_contents('php://input');
        break;
        default:
            echo '{"message":"This is a place holder."}';
        break;
    }

What's the deal!?这是怎么回事!?

My last answer was removed by a moderator, so here's the answer again;我的最后一个答案被版主删除了,所以这里又是一个答案;

For some reason, it gets mad about the JSON header, despite the fact all data is ALWAYS supposed to be JSON, and was used that way in testing.出于某种原因,它对 JSON 标头感到生气,尽管事实上所有数据都应该是 JSON,并且在测试中以这种方式使用。

So, I had to remove this part:所以,我不得不删除这部分:

header("Content-Type: application/json");

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

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