Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Revolution's avatar

<? PHP tag prepend on all AJAX responses --- RouteServiceProvider Solution

Hi All,

The following expands upon this thread by @jtn.

Running 5.3 and Vue 2.0, I have had a consistent issue in all AJAX responses wherein <? PHP was prepended to all response objects. I had a difficult time figuring out where the issue was. Being a bit of a novice on the api/ajax side, I thought it was my error, perhaps having to do with my controller method, perhaps not sending json properly, or in my Vue component perhaps something there on my fetch data method. I tried to resolve this for hours before finding @jtn's thread. @bashy provides some useful advice as always in that thread as well.

The problem for me was in included files in both my web.php and api.php files as called from the RouteServiceProvider. I had constructed several empty files as placeholders for route groups that I knew I would need. I included these various route files in web and then api.php as appropriate.

I had 2 files wherein I had no data at all, just a blank file, i.e.:

In web.php, I had "include('admin_routes/admin_products_routes.php');" and in this file, I had no text. Obviously this was a mistake, but no major errors were thrown by Laravel on this. As @jtn discovered, if you simply ensure that you have an open php tag "<?php " and something else (like a comment on a new line), then your ajax or vue resource response object (at least for me) will not have the <?php prepended to every response. I'm now getting a proper response object.

So, for what it's worth, if anyone else has this issue, look for any referenced files in RouteServiceProvider or web.php or api.php, ensure that each have the open php tag and at least ONE non-empty line like this:

0 likes
0 replies

Please or to participate in this conversation.