ReactJS: quot;Uncaught SyntaxError: Unexpected token lt;quot;(ReactJS:“未捕获的语法错误:意外的令牌 lt;)
问题描述
I am trying to get started building a site in ReactJS. However, when I tried to put my JS in a separate file, I started getting this error: "Uncaught SyntaxError: Unexpected token <".
I tried adding /** @jsx React.DOM */
to the top of the JS file, but it didn't fix anything. Below are the HTML and JS files. Any ideas as to what is going wrong?
HTML
<html>
<head>
<title>Page</title>
<script src="aHR0cDovL2ZiLm1lL3JlYWN0LTAuMTIuMi5qcw=="></script>
<script src="aHR0cDovL2ZiLm1lL0pTWFRyYW5zZm9ybWVyLTAuMTIuMi5qcw=="></script>
<script src="aHR0cDovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMC4wLm1pbi5qcw=="> </script>
<script src="Li9sYW5kZXIuanM="> </script>
</head>
<body>
<div id="content"></div>
<script type="text/jsx">
React.render(
<Lander />,
document.getElementById('content')
);
</script>
</body>
</html>
JS
/**
* @jsx React.DOM
*/
var Lander = React.createClass({
render: function () {
var info = "Lorem ipsum dolor sit amet... ";
return(
<div>
<div className="info">{info}</div>
</div>
);
}
});
EDIT: I realized that I need to add type="text/jsx"
to the script tag which includes my lander code. However, after adding this and reloading I get this warning
"You are using the in-browser JSX transformer. Be sure to precompile your JSX for production - http://facebook.github.io/react/docs/tooling-integration.html#jsx"
followed by this error:
"XMLHttpRequest cannot load file:///Users/.../lander.js. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource."
it seems like there is something else that I need to do in order to get in browser jsx transform working, but I'm not sure what it is.
EDIT: OOOOH do I need to host it using MAMP or something?
UPDATE -- use this instead:
<script type="text/babel" src="Li9sYW5kZXIuanM="></script>
Add type="text/jsx"
as an attribute of the script
tag used to include the JavaScript file that must be transformed by JSX Transformer, like that:
<script type="text/jsx" src="Li9sYW5kZXIuanM="></script>
Then you can use MAMP or some other service to host the page on localhost so that all of the inclusions work, as discussed here.
Thanks for all the help everyone!
这篇关于ReactJS:“未捕获的语法错误:意外的令牌 <"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ReactJS:“未捕获的语法错误:意外的令牌 <"


- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01