简体   繁体   English

如何修复“未捕获的语法错误:意外的令牌<”

[英]How to fix 'Uncaught SyntaxError: Unexpected token <'

I have a piece of Javascript code that worked before on PHP5.6 However, when I upgraded to PHP7.2 it stopped working and getting this error below:我有一段以前在 PHP5.6 上工作过的 Javascript 代码但是,当我升级到 PHP7.2 时,它停止工作并出现以下错误:

VM505:2 Uncaught SyntaxError: Unexpected token <
    at Function.globalEval (jquery.tools.min.js:49)
    at Function.httpData (jquery.tools.min.js:152)
    at XMLHttpRequest.x.onreadystatechange (jquery.tools.min.js:149)
globalEval  @   jquery.tools.min.js:49
httpData    @   jquery.tools.min.js:152
x.onreadystatechange    @   jquery.tools.min.js:149
XMLHttpRequest.send (async)     
ajax    @   jquery.tools.min.js:150
chkpwd  @   hw.php:295
onclick @   hw.php:877

I can't figure out that the error said chkpwd @ hw.php 295, but this function is actually in LINE 387. So, I don't know where to look into.我想不通那个错误是chkpwd@hw.php 295,但是这个函数实际上在LINE 387。所以,我不知道去哪里查看。

What's the best way to debug this and fix it?调试和修复它的最佳方法是什么?

function chkpwd() {

  var pwd_val = $("#engpassword").val();

  $.ajax({
    url: 'password.php',
    dataType: 'jsonp',
    jsonp: 'processData',
    data: {
      pwd: pwd_val,
      callback: 'pwdaction'
    },
    error: function(xhr) {
      alert('Ajax request error!');
      $(e.target).attr('disabled', false);
    }
  }); 
}

通常它指的是一个响应,而不是一个可解析的 JSON 返回(IE)一个 HTML,其中开始标记以<

If you are working with React app and you see this error in your console, it might be due to the missing "type" attribute of the script tag in your index.html file.如果您正在使用 React 应用程序并且在控制台中看到此错误,则可能是由于您的 index.html 文件中缺少脚本标记的“type”属性。 Make sure to include type="text/jsx".确保包含 type="text/jsx"。

Also, the URL that you are trying to access is not returning Javascript or JSON but HTML, give the correct path to the "src" attribute of your script tag beginning with "/" followed by the path of the .js file.此外,您尝试访问的 URL 不是返回 Javascript 或 JSON 而是 HTML,请为您的脚本标签的“src”属性提供正确的路径,以“/”开头,后跟 .js 文件的路径。

I took reference from the following link, do take a look as I have just briefed what was written in this article: https://idiallo.com/javascript/uncaught-syntaxerror-unexpected-token#n我参考了以下链接,请看一下,因为我刚刚简要介绍了本文中的内容: https : //idiallo.com/javascript/uncaught-syntaxerror-unexpected-token#n

PS - Go through the comments section within the article PS - 浏览文章中的评论部分

In my case ( Webpack v.5 + React ) it was happen on pages with 2nd or more nested level of pages.在我的情况下(Webpack v.5 + React)它发生在具有第二级或更多嵌套级别页面的页面上。 All assets are included using relative path like sct="./index.js" .所有资产都使用相对路径包含,如sct="./index.js" So for nested pages become broken.所以对于嵌套页面变得破碎。

To fix it need to add <base href="http://yoursitename.com">要修复它需要添加<base href="http://yoursitename.com">

If you are using webpack you can add:如果您使用的是 webpack,则可以添加:

const htmlWebpackPlugin = new HtmlWebpackPlugin({
  template: path.resolve(__dirname, 'src', 'index.html'),
  title: WebSiteName,
  base: isLocal ? 'http://localhost:4008' : 'http://yoursitename.com',
});

which will inject the element <base href="...">这将注入元素<base href="...">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM