How to resolve SyntaxError: Unexpected token < in JSON during Import

Tips submitted by PHPMaker users
Post Reply
mobhar
User
Posts: 11717

How to resolve SyntaxError: Unexpected token < in JSON during Import

Post by mobhar »

In the last two days, I encountered the strange error while importing data using Import feature. Here is the error message I saw in the Import dialog window: SyntaxError: Unexpected token < in JSON at position 0

I am really sure, that there is no wrong syntax/code I wrote related the Import server event. But the strange is, that error was suddenly just happened.

Once upon a time, that error is gone. Hey? What I did?

Well, I deleted ALL the generated script files, and then re-generate ALL the script files from stratch again.

After investigating the problem, i found out that the cause of issue is... if we generate the script files and then the package were not successfully complete downloaded (by enabling Composer update), then that error above was happened. Even the previous generating process was successfully.

So, for those of you who encounter that problem, the solution is just simple:
1. Just remove ALL, yes... ALL the generated script files.
2. After that, re-generate ALL the script files again from scratch.

That's it! When you run the Import data again, then everything is just back to normal. The error is gone.

So glad I found out this solution after struggling for two days. Hopefully this would help you, too.


arbei
User
Posts: 9379

Post by arbei »

When there is a PHP error, the data returned from server will be the PHP error message, not the expected JSON, so the JavaScript cannot parse it and hence the syntax error. When this error happens, you may try:

  1. Click Tools -> Advanced Settings, enable Debug, generate the config.php again so that the PHP error message will contain the full error message,
  2. Press F12 in Chrome, go to the Netowork panel, find the HTTP request with error, check the HTTP response from server, (see View a response body), you should be able to see the PHP error there.

mobhar
User
Posts: 11717

Post by mobhar »

After investigating the issue deeper, I think I've just found out the cause of it.

If we enable Compile container for production option and choose Production from Tools -> Advanced Settings, then the first Import was succeeded. But, when I try to do Import again, then it will return Server error parsererror: SyntaxError: Unexpected token < in JSON at position 0 error message.

I saw this following code from Network section after pressing F12:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /demo2021/api/upload</title>
 </head>
 <body>
<h1>Index of /demo2021/api/upload</h1>
  <table>
   <tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr>
   <tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/back.gif" alt="[PARENTDIR]"></td><td><a href="/demo2021/api/">Parent Directory</a>       </td><td>&nbsp;</td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="cache/">cache/</a>                 </td><td align="right">2020-11-07 08:53  </td><td align="right">  - </td><td>&nbsp;</td></tr>
   <tr><th colspan="5"><hr></th></tr>
</table>
<address>Apache/2.4.41 (Win64) PHP/7.4.0 Server at localhost Port 80</address>
</body></html>

So, that's the main cause of this error. It seems the system will read that HTML code above.

Then I saw in the generated sub-folder: /demo2021/api/upload/cache there is a file generated named CompiledContainer.php. The same thing also for the generated sub-folder: /demo2021/upload/cache, I saw the same CompiledContainer.php file generated.

Not sure why those /upload/cache sub-folder under the api sub-folder will cause the issue.

If I deleted the upload sub-folder under the api sub-folder, disabled Compile container for production option, and re-generate ALL the script files again, then the error is gone. Everything is back to normal, the Import feature is working properly in the generated web application.

Thoughts?


mobhar
User
Posts: 11717

Post by mobhar »

The conclusion that I can get from this situation is:

  1. If you enable Import feature, make sure you do not enable Compile container for production from Tools -> Advanced Settings. Otherwise, you will always get the error message above.

  2. If you have ever enabled Compile container for production option before re-generating the script files again, then make sure you have deleted the generated upload sub-folder under the api sub-folder in order to make sure the Import feature will work properly.

  3. Last but not least, make sure again you have already disabled Compile container for production option above, before re-generating ALL the script files again, and make sure you have already deleted the upload sub-folder under the api sub-folder, too.


arbei
User
Posts: 9379

Post by arbei »

Then I saw in the generated sub-folder: /demo2021/api/upload/cache there is a file generated named CompiledContainer.php

If the physical folder api/upload/ exists, it will override the API action "upload" route (also api/upload/), hence the error. You may change the upload folder name to another name.


mobhar
User
Posts: 11717

Post by mobhar »

Thanks. That did the trick.

Final Conclusion:

If you use Import feature, and enable Compile container for production, and choose production from Environment setting under Tools -> Advanced Settings, then make sure to change the upload/ to uploads/ from PHP -> General Options -> File Upload -> Upload folder before re-generating ALL the script files.

This will:

  1. Avoid override the API action upload route, and also api/upload/.
  2. Avoid error message while Importing data from the generated web applications: SyntaxError: Unexpected token < in JSON at position 0.

Post Reply