[SOLVED] Can't seem to return pure JSON from controller to frontend

Hey,
I’m trying to build a controller action that reads and uses data submitted via a <form>, and returns JSON data as a response. Context: registering for events.

I’ve tried several different approaches, but I’m unable to get a clean JSON output.

This is what I’ve tried:

  • returning new JsonResponse()
  • returning $this->responseFactory->createResponse() with correct header
  • returning plain json_encode()
  • returning $this→view→jsonResponse()
  • creating a custom page type and appending the pageNum to the autogenerated <form> action=””

await fetch(form.action + "&type=420420001", {
method: "POST",
body: formData,
headers: {
"X-Requested-With": "XMLHttpRequest"
}
})

tx_t3events_registerResponseJson = PAGE
tx_t3events_registerResponseJson {
typeNum = 420420001
config {
disableAllHeaderCode = 1
additionalHeaders.10.header = Content-Type: application/json
no_cache = 1
admPanel = 0
}
}

The first 4 options return a “fake JSON” response, with my data being wrapped in my page shell HTML

When I append the pageNum as a URL parameter, I get a completely empty response.

Any ideas what I might be doing wrong?

Previously in backend context I got it working using new JsonResponse().

You might want to try throwinf the PropagateResponseException in an extbase action controller:

The example is for a download, but a JSON or any other HTTP response works too.

1 Like

With your TS, you don’t have any output on your page, which I guess is the problem. I suggest you render your form action URI with EXT:topwire or EXT:typoscript_rendering and output the JSON response only.