Javascript 意外的标记 < 在 HTML 的第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31529446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:48:37  来源:igfitidea点击:

Unexpected token < in first line of HTML

javascripthtmlangularjssyntax-erroruncaught-exception

提问by simi kaur

I have an HTML file :

我有一个 HTML 文件:

<!DOCTYPE HTML>
<html lang="en-US" ng-app="Todo">
<head>
    <meta charset="UTF-8">
    <title>DemoAPI</title>

  <meta name="viewport">

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>

<link rel="stylesheet" href="./Client/css/styling.css" />
<script type="text/javascript" src="core.js"></script>

</head>

The error says:

错误说:

Uncaught SyntaxError: Unexpected token <    core.js: 1

It shows the error at <!doctype html>of the app.html.

它显示了在错误<!doctype html>app.html

core.jslooks like this:

core.js看起来像这样:

angular.module('Todo', [])

.controller('mainController', function($scope, $http)
{
    $scope.formData = {};

    // get all and show them
    $http.get('/musicians')
        .success(function(data) {
            $scope.todos = data;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });

        //get with an id
        $scope.getOneTodo = function() {
        $http.get('/musicians' + id)
            .success(function(data) {
                $scope.todos = data;
                      console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };
      // send the text to the node API
    $scope.createTodo = function() {
        $http.post('/musicians', $scope.formData)
            .success(function(data) {
                $scope.formData = {}; // clear the form 
                $scope.todos = data;
                console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            })
    };

    // delete 
    $scope.deleteTodo = function(id) {
        $http.delete('/musicians' + id)
            .success(function(data) {
                $scope.todos = data;
                      console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };

    /*
    $scope.updateTodo = function(id) {
        $http.delete('/musicians' + id)
            .success(function(data) {
                $scope.todos = data;
                      console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };*/

});

It also gives me Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.14/$injector/modulerr?p0=Todo&p1=Error%3A%2…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.14%2Fangular.min.js%3A17%3A381)

它也给了我 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.14/$injector/modulerr?p0=Todo&p1=Error%3A%2…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.14%2Fangular.min.js%3A17%3A381)

Besides, in console, when I click at core.js, it shows the contents of app.htmland name it core.js.

此外,在控制台中,当我单击 at 时core.js,它会显示 的内容app.html并为其命名core.js

Here is the snapshot:

这是快照:

Showing app.html instead of core.js

显示 app.html 而不是 core.js

Also, as in the image, when I click index.html, it shows app.html. However, I do not have any file that is named index.htmland I load app.htmlby default instead of index.html.

此外,如图所示,当我单击 时index.html,它会显示app.html。但是,我没有任何命名的文件,index.htmlapp.html默认加载而不是index.html.

I have tried adding/removing type="text/javascript"but no help with that either.

我试过添加/删除,type="text/javascript"但也没有帮助。

Also, status 200 is returned on get request for core.js.

此外,状态 200 在获取请求时返回core.js

Status: 200

状态:200

What might be wrong?

可能有什么问题?

回答by John Slegers

Your page references a Javascript file at /Client/public/core.js.

您的页面在/Client/public/core.js.

This file probably can't be found, producing either the website's frontpage or an HTML error page instead. This is a pretty common issue for eg. websites running on an Apache server where paths are redirected by default to index.php.

可能找不到此文件,而是生成网站的首页或 HTML 错误页面。这是一个很常见的问题,例如。在 Apache 服务器上运行的网站,其中路径默认重定向到index.php.

If that's the case, make sure you replace /Client/public/core.jsin your script tag <script type="text/javascript" src="/Client/public/core.js"></script>with the correct file path or put the missing file core.jsat location /Client/public/to fix your error!

如果是这种情况,请确保使用正确的文件路径替换/Client/public/core.js脚本标记<script type="text/javascript" src="/Client/public/core.js"></script>或将丢失的文件放在适当core.js的位置/Client/public/以修复错误!

If you do already find a file named core.jsat /Client/public/and the browser still produces a HTML page instead, check the permissions for folder and file. Either of these might be lacking the proper permissions.

如果您已经找到一个名为core.jsat的文件/Client/public/并且浏览器仍然生成一个 HTML 页面,请检查文件夹和文件的权限。其中任何一个都可能缺乏适当的权限。

回答by Carl

I experienced this error with my WordPress site but I saw that there were two indexes showing in my developer tools sources.

我在我的 WordPress 网站上遇到了这个错误,但我看到在我的开发人员工具源中显示了两个索引。

Chrome Developer Tool ErrorSo I had the thought that if there are two indexes starting at the first line of code then there's a replication and they're conflicting with each other. So I thought that then perhaps it's my HTML minification from my caching plugin tool.

Chrome 开发者工具错误所以我认为如果从第一行代码开始有两个索引,那么就会有一个复制,并且它们彼此冲突。所以我想那也许是我的缓存插件工具的 HTML 缩小。

So I turned off the HTML minify setting and deleted my cache. And poof! It worked!

所以我关闭了 HTML 缩小设置并删除了我的缓存。还有噗!有效!

回答by seg-s

Check your encoding, i got something similar once because of the BOM.

检查你的编码,因为 BOM,我得到了类似的东西。

Make sure the core.js file is encoded in utf-8 without BOM

确保 core.js 文件以 utf-8 编码,没有 BOM

回答by Alex

In my case I got this error because of a line

就我而言,由于一行,我收到此错误

<script src="#"></script> 

Chrome tried to interpret the current HTML file then as javascript.

Chrome 尝试将当前的 HTML 文件解释为 javascript。

回答by Alisson

I had this problem in an ASP.NET application, specifically a Web Forms.

我在 ASP.NET 应用程序中遇到了这个问题,特别是 Web 窗体。

I was forcing a redirect in Global.asax, but I forgot to check if the request was for resources like css, javascript, etc. I just had to add the following checks:

我在Global.asax中强制重定向,但我忘了检查请求是否是针对 css、javascript 等资源的。我只需要添加以下检查:

VB.NET

VB.NET

If Not Response.IsRequestBeingRedirected _
    And Not Request.Url.AbsoluteUri.Contains(".WebResource") _
    And Not Request.Url.AbsoluteUri.Contains(".css") _
    And Not Request.Url.AbsoluteUri.Contains(".js") _
    And Not Request.Url.AbsoluteUri.Contains("images/") _
    And Not Request.Url.AbsoluteUri.Contains("favicon") Then
        Response.Redirect("~/change-password.aspx")
End If

I was forcing logged users which hadn't change their passwords for a long time, to be redirected to the change-password.aspxpage. I believe there is a better way to check this, but for now, this worked. Should I find a better solution, I edit my answer.

我强迫长时间未更改密码的登录用户重定向到change-password.aspx页面。我相信有更好的方法来检查这一点,但就目前而言,这是有效的。如果我找到更好的解决方案,我会编辑我的答案。

回答by Roy Segall

Well... I flipped the internet upside down three times but did not find anything that might help me because it was a Drupal project rather than other scenarios people described.

嗯......我把互联网颠倒了三遍,但没有找到任何可能对我有帮助的东西,因为它是一个 Drupal 项目,而不是人们描述的其他场景。

My problem was that someone in the project added a js which his address was: <script src="http://base_url/?p4sxbt"></script>and it was attached in this way:

我的问题是项目中有人添加了一个js,他的地址是:<script src="http://base_url/?p4sxbt"></script>并且是这样附加的:

drupal_add_js('',
    array('scope' => 'footer', 'weight' => 5)
  );

Hope this will help someone in the future.

希望这会在将来对某人有所帮助。

回答by Mr. de Silva

We had the same problem sometime ago where a site suddenly began giving this error. The reason was that a js include was temporarily remarked with a # (i.e. src="#./js...").

前一段时间我们遇到了同样的问题,一个网站突然开始出现这个错误。原因是一个 js 包含被临时标记为 #(即 src="#./js...")。

回答by S.Sire

I had the same issue. I published the angular/core application on iis.

我遇到过同样的问题。我在 iis 上发布了 angular/core 应用程序。

To change the Identity of the application pool solved my issue. Now the Identity is LocalSystem

更改应用程序池的标识解决了我的问题。现在身份是 LocalSystem

回答by Antony

For me this was a case that the Script path wouldn't load - I had incorrectly linked it. Check your script files - even if no path error is reported - actually load.

对我来说,这是脚本路径无法加载的情况 - 我错误地链接了它。检查您的脚本文件 - 即使没有报告路径错误 - 实际加载。