Hi,
I'm having problems getting the body content of my promise post using CI4.
No matter how I configure the await fetch() call, file_get_contenys('php://input) is empty.
Any help with this would be greatly appreciated?
My route
PHP Code:
$routes->post('s_pay_form', 'Signup\Payments');
My Javascript
Code:
<script>
// The items the customer wants to buy
const items = [{ id: "xl-tshirt" }];
initialize();
// Fetches a payment intent and captures the client secret
async function initialize() {
const { clientSecret } = await fetch("s_pay_form", {
method: "POST",
//headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items }),
}).then(
data => { alert(JSON.stringify(data)) },
error => { alert(error) }
);
</script>
Controller
PHP Code:
<?php namespace App\Controllers\Signup;
use CodeIgniter\Controller;
use CodeIgniter\Database\Query;
use App\Controllers\BaseController;
class Payments extends BaseController
{
public function index()
{
function calculateOrderAmount(array $items): int {
return 1400;
}
header('Content-Type: application/json');
try {
// retrieve JSON from POST body
$jsonStr = file_get_contents('php://input');
return json_encode($jsonStr);
} catch (Error $e) {
http_response_code(500);
return json_encode(['error' => $e->getMessage()]);
}
}
}
Result = ""