0

Due to some other issues I have replaced a "standard" POST request with JSON based POST request via Fetch API. After some help I got the server side running. However, as soon as I read an element from my results array, I receive an error.

POST REQUEST

fetch('/api/apicreatebooking.php', {
          method: 'POST',
          body: JSON.stringify({        
                'fromDate': document.getElementById('fromDate').value,
                'toDate': document.getElementById('toDate').value,
                'apptmnt': document.querySelector('input[name="unit"]:checked').value,
                'total': document.getElementById("total").value,
                'lan': document.getElementById("lan").value
          })
})
.then(function(response){ 
        return response.json()})
.then(function(data){
    console.log(data)
});

SERVER SIDE PHP FOR POST REQUEST

$json = file_get_contents("php://input");
$data = json_decode($json);
$a="a>";

//if (isset($data['total'])) {$a += $data['total'];}

$a=print_r($data,true);
$result = array(
    "success" => true,
    "message" => $a,
    "bookingCode" => '1234567'
);

$output = json_encode($result);
echo $output;

The code as stated above returns in the console

Object

bookingCode: "1234567"

message: "stdClass Object↵(↵ [fromDate] => 2022-07-02↵ [toDate] => 2022-07-09↵ [apptmnt] => 2↵ [total] => 11340 €↵ [lan] => DE↵)↵"

success: true

However, as soon as I swap the comment on the server side (if (isset...) to be code Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:8888/api/apibooking.php and Error: SyntaxError: The string did not match the expected pattern.

w461
  • 2,168
  • 4
  • 14
  • 40
  • what is $a="a>"? – Your Common Sense Jul 24 '22 at 11:58
  • In case of 500 error you have to look into php error log for the error message. – Your Common Sense Jul 24 '22 at 12:00
  • @YourCommonSense $a="a>" is the variable that I return with the request. And I profile this with something, so that something is returned, when the data I add to $a is empty. Not sure, whether you closed the question - however, I think with POST request landing page I hardly get any PHP errors displayed since the page doesn't output anything to the client. I didn't even get the console.log to work on this landing page – w461 Jul 24 '22 at 12:22
  • Nobody said console.log. You need the error log **on the server.** You are getting 500 error from the server, and, therefore, have to look into the error logs on the server. – Your Common Sense Jul 24 '22 at 12:54
  • Does it make any sense to add some number to a value like `a>`? What result you expect? – Your Common Sense Jul 24 '22 at 12:56
  • if it is int, maybe not. Anyhow, I now managed to find the error log file (at a different spot than definied in php.ini) and I found the issue. Now I even know hoe to check this log :) – w461 Jul 24 '22 at 14:26

0 Answers0