2
votes

I'm currently facing a strange problem and I cannot narrow down the issue.

In my Laravel application I am retrieving data via AJAX. Therefore parts of the application respond with the application/json content-type.

return Response::json(['message' => 'Some message']);

on my local webserver everything works just fine. I get Content-Type:application/json as a response.

I deployed the application on the server (also apache) and tested it. But here is the problem. I do not get the proper content-type anymore. Instead I get text/html.

So there's a bunch of possible problem sources and I am not too much into server configuration. But here are the things I already tried:

  • Made sure that the server has the json extension and it is enabled
  • Checked the laravel source for hints why it would not return the proper content/type (I couldn't find anything)
  • Checked the response contents locally and on the server (it does not differ)
  • I noticed that the Response-Header contains locally Set-Cookie which contains: laravel_session=[...] whereas the response on the server does not.

Please refrain from posting answers that suggest to check for the content-type and parse it if necessary on the client side, because although that would be possible, the server should not behave like that and I'm trying to figure out why

2
Did you manage to solve this?? I'm having the exact same issue. The strange thing is I have other laravel applications in my server and they run just fine.Javier Enríquez
Unfortunately I did not. I ended up parsing it as json client-sidethpl

2 Answers

1
votes

This does sound more like a server configuration problem, especially since it's working fine on your local environment on not on your production environment. I know you made sure that the server has the json extension and it is enabled, but you might want to double check the following on your production server anyway:

  • Do you have the AddType application/json .json directive in you Apache configuration (at /etc/apache2/mods-available/mime.conf)?
  • Do you have mod mime enabled (can be done via a2enmod mime)?
0
votes

Perhaps this answer comes to late but yesterday I ran into this problem and actually kind of solved it. I don't really know why this happens but I noticed that whenever I ran artisan serve or any other artisan command a strange tab appeared before all the command messages. So a open all of my controllers and found that there was a tab before a php opening tab. So I had something like this:

    <?
class MyController extends BaseController{
....
}

That tab was there by accident but seems it was causing the problem. As soon as I removed it the Content-Type returned by all of my Response::json calls returned to be application/json

Hope this helps even if it is too late.