Accept header not working #3483
Answered
by
RomainLanz
feniljariwala82
asked this question in
Help
-
According to the documentation: show, I tried to add a header in Ajax request. The thing is I want a JSON response from the same controller which renders the index page. Here you can see the header code provided below. After providing Accept header in the Ajax request. The controller is returning the default view. Can you please guide me on what I am doing wrong here? |
Beta Was this translation helpful? Give feedback.
Answered by
RomainLanz
Jan 12, 2022
Replies: 1 comment 1 reply
-
Hey @feniljariwala82! 👋🏻 First of all, the correct type should be Route.get('posts', async ({ request, view }) => {
const posts = [
{
title: 'Adonis 101',
},
]
switch (request.accepts(['html', 'json'])) {
case 'html':
return view.render('posts/index', { posts })
case 'json':
return posts
default:
return view.render('posts/index', { posts })
}
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
feniljariwala82
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @feniljariwala82! 👋🏻
First of all, the correct type should be
application/json
.Secondly, you must handle that yourself using Content Negotiation.