简体   繁体   English

使用Chrome:未捕获的SyntaxError:意外令牌

[英]With Chrome: Uncaught SyntaxError: Unexpected token <

I am getting this error Uncaught SyntaxError: Unexpected token < from chrome but everything works fine with FireFox. 我收到此错误Uncaught SyntaxError:来自chrome的意外令牌< ,但在FireFox上一切正常。 I have found many similar posts but no solution. 我发现了许多类似的帖子,但没有解决方案。

So I am wondering if there is a way of sending a second page to the browser after it has been build, with it's own header. 因此,我想知道是否有一种方法可以在构建完第二个页面之后使用它自己的标题将第二个页面发送到浏览器。 The idea came, when I saw that Firefox places, what I echo in the function below, after the html closing tag 这个想法来了,当我看到Firefox放置了html结束标记后,我在下面的函数中回显了什么 , versus Chrome places it before the closing tag. ,而Chrome则将其放在结束标记之前。

Basically, I like to send in this order: 基本上,我喜欢按以下顺序发送:

header('Content-Type: text/html; charset= utf-8');
<html></html>

header('Content-Type: text/javascript; charset= utf-8'); 
<script></script>

This is my php script, I would like to uncomment the header code and send it independently from the html page. 这是我的php脚本,我想取消注释标头代码并独立于html页面发送它。

public static function jsShow($html)
{
    //header('Content-Type: text/javascript; charset= utf-8');
    echo "
        <script type=\"text/javascript\">
        var e = document.getElementById('message');
        e.innerHTML = $html ;
        e.style.display = 'block';
        </script>";
}

This is what the page looks like in Firefox, and this works: 这是该页面在Firefox中的外观,并且可以正常工作:

</body>
</html>

<script type="text/javascript">
var e = document.getElementById('message');
e.innerHTML = <ul style="list-style: none; margin: 0; padding: 0;">
<li style="background-color: #0000FF; margin: 0;"><img src="/asset/icon/info.gif" alt="Info: " /> working</li>
<li style="background-color: #008000; margin: 0;"><img src="/asset/icon/success.gif" alt="Success: " /> Got it</li>
</ul>
 ;
e.style.display = 'block';

</script>

I thought that I can maybe use ob_start() & ob_end_flush(), but you can't control headers with that only content. 我以为我可以使用ob_start()和ob_end_flush(),但是您不能仅使用内容来控制标头。

...

        e.innerHTML = " . json_encode($html) . " ;

The error "Uncaught SyntaxError: Unexpected token <" is an appropriate error for what you are showing in your second code block, because it is invalid JavaScript and should fail in all browsers. 对于第二个代码块中显示的错误,错误“未捕获的SyntaxError:意外的令牌<”是一个适当的错误,因为它是无效的JavaScript,并且在所有浏览器中都将失败。 You are assigning e.innerHTML equal to an unquoted string: 您要分配e.innerHTML等于未引用的字符串:

e.innerHTML = <ul style="list-style: none; margin: 0; padding: 0;">
<li style="background-color: #0000FF; margin: 0;"><img src="/asset/icon/info.gif" alt="Info: " /> working</li>
<li style="background-color: #008000; margin: 0;"><img src="/asset/icon/success.gif" alt="Success: " /> Got it</li>
</ul>
 ;

See the "<" right after the "="? 看到“ =”之后的“ <”吗? - that is the unexpected token. -这是意外令牌。

You don't seem to use any single quotes within that string, so the simplest fix is just to wrap it in single quotes. 您似乎没有在该字符串中使用任何单引号,因此最简单的解决方法是将其包装在单引号中。 I don't know PHP, but something like this: 我不知道PHP,但是类似这样的东西:

echo "
    <script type=\"text/javascript\">
    var e = document.getElementById('message');
    e.innerHTML = '" . $html . "';
    e.style.display = 'block';
    </script>";

Or whatever the PHP syntax is to get this result returned to the browser: 或使用任何PHP语法将结果返回给浏览器:

e.innerHTML = '<ul style="list-style ...   </ul>';

By the way, it doesn't make sense to set two different headers for the same response. 顺便说一句,为同一响应设置两个不同的标头没有任何意义。 What you are returning is one page that is html, which happens to have a script block at the bottom. 您返回的是一个html页面,该页面的底部恰好有一个脚本块。 The "text/javascript" content type is more for linked JS files, which do not contain html (no script tags), just JS. “文本/ javascript”内容类型更多地用于链接的JS文件,该文件不包含html(不包含脚本标签),而仅包含JS。

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

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