1👍
✅
You can’t respond to a single request with both an HTML document and a seperate JavaScript file.
So you need to either:
- Store the data somewhere and, in a separate endpoint, serve up some generated JavaScript and have a mechanism which identifies which data to serve up when the request is made. (NB: This is stupidly complex. Don’t do it.)
- Generate the JavaScript inline in the HTML document inside a
<script>
element. (You’ll need to properly escape the data as JS to make sure you aren’t subject to XSS). - Output the data into the HTML document (e.g. with
data-*
attributes or<meta>
elements) and read it with static JavaScript. - Again, using a seperate endpoint: Serve up JSON with the data in it and read that with Ajax from a static JS file. (Again, you’ll need a way to determine which data to send for that particular request).
Source:stackexchange.com