Api_Action post

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
sangnandar
User
Posts: 1071

Api_Action post

Post by sangnandar »

This code works on v2024, but doesn't work on v2025.

// server side
function Api_Action(RouteCollectorProxyInterface $app): void
{
	$app->post('/test', function ($request, $response, $args) {
		$val = $request->getParsedBody();
		Log('test', ["request" => $val, "args" => $args]);
		return $response->withJson(["request" => $val['name']]);
	});
}
// client side
value = {
  name: 'John',
  age: 30
};
$.post(ew.getApiUrl(['test']), value, function(res) {
  console.log(res);
});

sangnandar
User
Posts: 1071

Post by sangnandar »

The error from DevTools->Network is: 400 Bad Request

Tried with $.ajax(), same result (400 Bad Request)

$.ajax({
	url: ew.getApiUrl(['test']),
	type: 'POST',
	contentType: 'application/json',
	data: JSON.stringify(value), <-- with or without JSON.stringify()
	success: function(response) {
		console.log(response);
	},
	error: function(xhr, status, error) {
		console.error('Error:', error);
	}
});

Post Reply