-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Set correct Content-Type header for ajax responses in the Magento_UI module. #10521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set correct Content-Type header for ajax responses in the Magento_UI module. #10521
Conversation
@@ -30,6 +30,7 @@ public function execute() | |||
$component = $this->factory->create($this->_request->getParam('namespace')); | |||
$this->prepareComponent($component); | |||
$this->_response->appendBody((string) $component->render()); | |||
$this->_response->setHeader('Content-Type', 'application/json', true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main problem with hard-coding Content-Type
here is that the $component->render()
has multiple types of renderers.
You may refer to \Magento\Ui\Component\AbstractComponent::render
which in place selects the render engine from the classes which implement \Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeInterface
.
This list is not limited to Json, but also has an Html and Xml implementations out of the box.
I suggest looking into different approach to solve this issue, such as:
- Possibly limiting the
Content-Type
header to the correct renderer - Working with the
Accept
header provided by client to ensure the correct response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I wasn't sure if all responses were in json format (didn't take my time to look it through, my mistake).
I'll have a look at a better solution.
3752054
to
1e3181c
Compare
I force pushed a new commit, this is probably a better way to handle it. Comments are welcome. |
1e3181c
to
d0b85b3
Compare
…into use-json-content-type-header-for-ajax-responses
…the Magento_UI module
Hi @hostep Please let me know what you think of this. |
…the Magento_UI module
@ishakhsuvarov: nice, looks good. So it looks like it is now based on the Good job, and thank you! :) |
|
…s in the Magento_UI module. #10521
[EngCom] Public Pull Requests - MAGETWO-72390: Remove the usage of the DataObject for response management #10808 - MAGETWO-71545: Added 'application/json' Content-Type to Ajax responses in the Magento_UI module. #10521 - MAGETWO-72388: Fix spelling mistake in AddressTest.php #10806 - MAGETWO-72283: Code generate: support variadic parameter #10771
…ontent-Type to Ajax responses in the Magento_UI module. magento#10521 (cherry picked from commit ddfc01e)
Description
We started noticing issues when New Relic's Browser profiling was enabled, which inserts a javascript block into
text/html
responses whenever a<head>
tag was found in such a response.This has the possibility to break certain ajax responses in Magento's backend, since they have Content-Type:
text/html
instead ofapplication/json
.I'm not sure if this fix is the correct fix, and if there aren't other violations in the code for this same problem. Feel free to let me know if that should be the case and I'll try to add them to the PR.
Fixed Issues (if relevant)
None that I could find
Manual testing scenarios

6. Inspecting the response, it contains invalid json, because New Relic inserted its javascript in there:Changing the Content-Type to
application/json
no longer makes New Relic insert its javascript into the response and the grid loads perfectly fine.Contribution checklist