javascript - Spring mvc loading JS with Uncaught SyntaxError: Unexpected token < -
i'm using spring boot. have structure simple test app:
what in testcontroller 2 path variables , load index.html:
@controller public class testcontroller { @requestmapping("/{vara}/{varb}") public string index(@pathvariable(value="vara") string vara, @pathvariable(value="varb") string varb) { return"/index.html"; } } and index.html empty, clean html page:
<!doctype html> <html> <head> <title>insert title here</title> </head> <body> test </body> </html> so typing localhost:8080/abbas/mirza in browser , looks ok , html page loads easily. have 2 js files, test.js , /js/testb.js both have line of code inside.
var s = {}; now add
<script src="/test.js"></script> and still ok , can view test.js code, when add
<script src="/js/testb.js"></script> the page throws exception
uncaught syntaxerror: unexpected token < and when try open testb.js file shows me this
if move testb.js webapp itself, loaded no problem. i'm quite newbie using spring mvc , still confused configuration regarding routings , accessing resources.
any idea i'm doing wrong?
edit:(link github) https://github.com/arashzz/test22
the @requestmapping pattern you're using broad path javascript file testb.js matches , index page served in lieu of js file causing syntax error occur.
controller:
@requestmapping("/{vara}/{varb}") javascript path:
/js/testb.js this matches pattern vara=js , varb=testb.js
your other javascript file located @ /test.js , doesn't match pattern , served correctly.
solution:
adjust @requestmapping pattern more narrow such as:
@requestmapping("/placeholder/{vara}/{varb}") 

Comments
Post a Comment