最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Understanding the error "SyntaxError: expected expression, got '<'" - Stac

programmeradmin5浏览0评论

I just wasted a lot of time trying to figure out why Firefox and Chrome were both telling me SyntaxError: expected expression, got '<' and refusing to load the script on my webpage. It turns out the issue was because I wrote

<script src="static/js/mon.js"></script>

instead of

<script src="/static/js/mon.js"></script>

Now I totally understand why missing the leading slash in the path would make it unable to find the script, but c'mon, surely the javascript terminal can do a better job at giving me an error. Why not just a 404 Not Found?

What is the logical reason it would give me such a misleading error? Maybe I can learn something from this experience and feel better about that huge time-suck I just endured.

I just wasted a lot of time trying to figure out why Firefox and Chrome were both telling me SyntaxError: expected expression, got '<' and refusing to load the script on my webpage. It turns out the issue was because I wrote

<script src="static/js/mon.js"></script>

instead of

<script src="/static/js/mon.js"></script>

Now I totally understand why missing the leading slash in the path would make it unable to find the script, but c'mon, surely the javascript terminal can do a better job at giving me an error. Why not just a 404 Not Found?

What is the logical reason it would give me such a misleading error? Maybe I can learn something from this experience and feel better about that huge time-suck I just endured.

Share Improve this question asked May 8, 2015 at 14:47 J-bobJ-bob 9,12613 gold badges57 silver badges89 bronze badges 3
  • 1 This sounds like your webserver returned the incorrect response (200 OK along with HTML content) rather than the browser doing something wrong. You should fix your webserver. – CodingIntrigue Commented May 8, 2015 at 14:49
  • if you click the error message in the console it should take you to the scripts tab of dev tools and you should see the 200 error doc and then know what had happened – chiliNUT Commented May 8, 2015 at 14:51
  • Are you sure it didn't find something? Is there actually a file at the address "static/js/mon.js"? It looks to me like it found something; just not what you wanted it to find. – Mark_1 Commented May 8, 2015 at 14:53
Add a ment  | 

1 Answer 1

Reset to default 6

It didn't get a 404 Not Found. The server sent back a 200 OK (which was probably a configuration error) and an HTML document which the browser attempted to parse as JavaScript.

Possibly it sent back a 302 and a redirect to the homepage which then gave the aforementioned 200 OK.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论