1

I used to do the following to add the content of a JSON file to my JS code, but since I changed my web host I get this error: "Uncaught SyntaxError: Unexpected token <"

var someVar = <?php include 'someJsonFile.geojson'; ?>;

Where did I go wrong?

3
  • You probably had your previous webhost set up to run .js files through the PHP interpreter before serving them. Commented Dec 3, 2015 at 22:42
  • Is there an easy fix to it? Commented Dec 3, 2015 at 22:45
  • That depends entirely on your webhost Commented Dec 3, 2015 at 22:57

2 Answers 2

1

One quick fix might be to modify an existing .htaccess file (or create a new .htaccess file) in the directory that houses your js file.

Try adding one of the following lines to the .htaccess file:

AddHandler x-httpd-php .html .htm .js
AddHandler php-script .php .html .htm .js
AddHandler php5-script .php .html .htm .js
AddType application/x-httpd-php .htm .js
AddType application/x-httpd-php .html .js

Notice the ".js" at the end? This will instruct the server to process js files through PHP.

Sign up to request clarification or add additional context in comments.

Comments

0

Probably your previous web host was running .js files through the php interpreter.

The fastest solution would be to include the javascript in the php file within a script tag.

...
<head>
   <script type="text/javascript">
      var someVar = <?php include 'someJsonFile.geojson'; ?>;
   </script>
</head>
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.