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
pageNumto 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().
