简体   繁体   中英

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.

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:

    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.

So, I had to remove this part:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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