Web Hosting - PHP cannot read incoming post data
Answers to your questions / Web Hosting / PHP cannot read incoming ...
BMPCreated with Sketch.BMPZIPCreated with Sketch.ZIPXLSCreated with Sketch.XLSTXTCreated with Sketch.TXTPPTCreated with Sketch.PPTPNGCreated with Sketch.PNGPDFCreated with Sketch.PDFJPGCreated with Sketch.JPGGIFCreated with Sketch.GIFDOCCreated with Sketch.DOC Error Created with Sketch.
Frage

PHP cannot read incoming post data

Von
MaciejT
Erstellungsdatum 2020-12-23 12:29:27 (edited on 2024-09-04 14:24:23) in Web Hosting

Hi,

I've created my own custom application in PHP and 2 hours ago sent it to my OVH hosting catalog. Ever since then it's been nothing but problems.

My frontend sends to the backend data from a form in the following way with jQuery:

let formData = $('#token-form').serialize();
var requestForTokens = $.ajax('file.php', {
method: 'post',
data: formData,
contentType: "application/json",
dataType: "json",
encoding: "text/plain"
});

(there's only one input named "access-token")

On the backend I have the following code to verify incoming data:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$postData = json_decode(file_get_contents('php://input'), true);
$token = $postData['access-token'] ?: null;
if (empty($token)) {
http_response_code(401);
$response['error_message']='Token cannot be empty';
//much more code later

Of course every request ends up with the empty token message.

Before I didn't have the 'contentType: "application/json"' in my ajax request and I was just using "$_POST['access-token']" to get contents of the incoming data, but the the server always responded with 400.

Please help. On my xampp localhost it worked perfectly, no problem. With OVH I constantly stumble upon every possible error.


Antworten sind derzeit für diese Frage deaktiviert.