Page 1 of 1

Api_Action post

Posted: Wed Oct 16, 2024 4:26 am
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);
});

Re: Api_Action post

Posted: Wed Oct 16, 2024 4:33 am
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);
	}
});

Re: Api_Action post

Posted: Wed Oct 16, 2024 11:04 am
by Webmaster

Click Tools -> Update Template and try again.