Jquery 未捕获语法错误
我有一个输出以下 HTML 的 JSP:
<html>
<head>
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
</head>
<body>
</body>
</html>
当我在 Chrome 中打开此页面时,我得到:
Uncaught SyntaxError: Unexpected token : jquery-1.4.3.min.js 120
jquery-1.4.3.min.js:-1Resource interpreted as script but transferred with MIME type application/octet-stream. jquery-1.4.3.min.js -1
当我在 IE 8 中打开时,我得到:
Message: Expected '}'
Line: 109
Char: 466
Code: 0
URI: http://localhost:8080/jquery-1.4.3.min.js
我在 JQuery 1.4.2 和 unminified 中遇到了类似的错误jQuery JavaScript 的版本。
请有人告诉我我做错了什么?
谢谢。
更新
我已经修复了我的 mime 类型,但仍然收到错误。现在它只是一个
Uncaught SyntaxError: Unexpected token :
以下是我的服务器上 jQuery 请求的响应标头:
Cache-Control:max-age=32400, s-maxage=0
Connection:Keep-Alive
Content-Length:75134
Content-Type:text/javascript; charset=UTF-8
Date:Fri, 12 Nov 2010 11:28:21 GMT
Expires:Sat, 13 Nov 2010 11:28:21 GMT
Keep-Alive:timeout=15, max=100
Last-Modified:Fri, 12 Nov 2010 11:16:53 GMT
Pragma:cache
Server:Oracle Application Server Containers for J2EE 10g (9.0.4.0.0)
Transfer-Encoding:chunked
来自 Google 文件的响应标头是:
Age:119
Cache-Control:public, max-age=31536000
Content-Encoding:gzip
Content-Length:26769
Content-Type:text/javascript; charset=UTF-8
Date:Fri, 12 Nov 2010 11:26:22 GMT
Expires:Sat, 12 Nov 2011 11:26:22 GMT
Last-Modified:Fri, 15 Oct 2010 18:25:24 GMT
Server:sffe
Vary:Accept-Encoding
X-Content-Type-Options:nosniff
现在内容类型对我来说看起来相同。
还有其他想法吗?
已解决 这个问题与 jquery、mime 类型等完全无关。 这是我们的 ant 构建文件。那里有一个目标(我们已经在这段代码上使用了 6 年多了),它试图使用正则表达式搜索/替换来最小化任何 JS 文件。这破坏了实际的 jQuery javascript 文件。噢!
I've got a JSP which is outputting the following HTML:
<html>
<head>
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
</head>
<body>
</body>
</html>
When I open this page in Chrome, I get:
Uncaught SyntaxError: Unexpected token : jquery-1.4.3.min.js 120
jquery-1.4.3.min.js:-1Resource interpreted as script but transferred with MIME type application/octet-stream. jquery-1.4.3.min.js -1
When I open is in IE 8, I get:
Message: Expected '}'
Line: 109
Char: 466
Code: 0
URI: http://localhost:8080/jquery-1.4.3.min.js
I've had similar errors with JQuery 1.4.2 and with the unminified versions of the jQuery JavaScript.
Please could someone show me what I'm doing wrong?
Thanks.
UPDATE
I've fixed my mime-types, but I still get an error. Now it is simply a
Uncaught SyntaxError: Unexpected token :
Here are the response headers from the jQuery request on my server:
Cache-Control:max-age=32400, s-maxage=0
Connection:Keep-Alive
Content-Length:75134
Content-Type:text/javascript; charset=UTF-8
Date:Fri, 12 Nov 2010 11:28:21 GMT
Expires:Sat, 13 Nov 2010 11:28:21 GMT
Keep-Alive:timeout=15, max=100
Last-Modified:Fri, 12 Nov 2010 11:16:53 GMT
Pragma:cache
Server:Oracle Application Server Containers for J2EE 10g (9.0.4.0.0)
Transfer-Encoding:chunked
The response headers from Google's file are:
Age:119
Cache-Control:public, max-age=31536000
Content-Encoding:gzip
Content-Length:26769
Content-Type:text/javascript; charset=UTF-8
Date:Fri, 12 Nov 2010 11:26:22 GMT
Expires:Sat, 12 Nov 2011 11:26:22 GMT
Last-Modified:Fri, 15 Oct 2010 18:25:24 GMT
Server:sffe
Vary:Accept-Encoding
X-Content-Type-Options:nosniff
The content types now look the same to me.
Any other ideas please?
SOLVED
The problem was nothing at all to do with jquery, mime types, etc.
It was our ant build file. There was a target in there (which we've had on this code for over 6 years) which tried to minimise any JS file using a regular expression search/replace. This was breaking the actual jQuery javascript file. D'oh!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的服务器可能配置错误,并且将 .js 文件作为
application/octet-stream
而不是text/javascript
发送。不过,我不确定为什么这会导致解析错误,就像您似乎遇到的那样。如果您使用 jQuery 的 Google CDN URL (http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js),您是否会遇到相同的错误?
It looks likely that your server is misconfigured, and is sending .js files as
application/octet-stream
instead of astext/javascript
. I'm not sure why this would cause parsing errors like the ones you seem to be getting, though.If you use the Google CDN URL for jQuery (http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js) do you get the same error?
问题之外的事情正在发生,其他一些事情包括干扰。这些不匹配:
请注意,它是 1.4.3,而您的错误是 1.4.2:
在 jQuery 包含之前检查另一个文件没有合适的右大括号。
You have something outside the question happening, some other includes interfering. These don't match up:
Note it's 1.4.3, while your error is 1.4.2:
Check for another file before your jQuery include that doesn't have a proper closing brace.