【发布时间】:2020-02-05 04:44:38
【问题描述】:
在处理类似 Facebook 的内容提要的 React 应用程序组件中,我遇到了一个错误:
Feed.js:94 undefined "parsererror" "SyntaxError: Unexpected token
我遇到了类似的错误,结果证明是渲染函数中的 HTML 中的拼写错误,但这里的情况似乎并非如此。
更令人困惑的是,我将代码回滚到更早的已知工作版本,但我仍然收到错误消息。
Feed.js:
import React from 'react';
var ThreadForm = React.createClass({
getInitialState: function () {
return {author: '',
text: '',
included: '',
victim: ''
}
},
handleAuthorChange: function (e) {
this.setState({author: e.target.value})
},
handleTextChange: function (e) {
this.setState({text: e.target.value})
},
handleIncludedChange: function (e) {
this.setState({included: e.target.value})
},
handleVictimChange: function (e) {
this.setState({victim: e.target.value})
},
handleSubmit: function (e) {
e.preventDefault()
var author = this.state.author.trim()
var text = this.state.text.trim()
var included = this.state.included.trim()
var victim = this.state.victim.trim()
if (!text || !author || !included || !victim) {
return
}
this.props.onThreadSubmit({author: author,
text: text,
included: included,
victim: victim
})
this.setState({author: '',
text: '',
included: '',
victim: ''
})
},
render: function () {
return (
<form className="threadForm" onSubmit={this.handleSubmit}>
<input
type="text"
placeholder="Your name"
value={this.state.author}
onChange={this.handleAuthorChange} />
<input
type="text"
placeholder="Say something..."
value={this.state.text}
onChange={this.handleTextChange} />
<input
type="text"
placeholder="Name your victim"
value={this.state.victim}
onChange={this.handleVictimChange} />
<input
type="text"
placeholder="Who can see?"
value={this.state.included}
onChange={this.handleIncludedChange} />
<input type="submit" value="Post" />
</form>
)
}
})
var ThreadsBox = React.createClass({
loadThreadsFromServer: function () {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function (data) {
this.setState({data: data})
}.bind(this),
error: function (xhr, status, err) {
console.error(this.props.url, status, err.toString())
}.bind(this)
})
},
handleThreadSubmit: function (thread) {
var threads = this.state.data
var newThreads = threads.concat([thread])
this.setState({data: newThreads})
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: thread,
success: function (data) {
this.setState({data: data})
}.bind(this),
error: function (xhr, status, err) {
this.setState({data: threads})
console.error(this.props.url, status, err.toString())
}.bind(this)
})
},
getInitialState: function () {
return {data: []}
},
componentDidMount: function () {
this.loadThreadsFromServer()
setInterval(this.loadThreadsFromServer, this.props.pollInterval)
},
render: function () {
return (
<div className="threadsBox">
<h1>Feed</h1>
<div>
<ThreadForm onThreadSubmit={this.handleThreadSubmit} />
</div>
</div>
)
}
})
module.exports = ThreadsBox
在 Chrome 开发者工具中,错误似乎来自这个函数:
loadThreadsFromServer: function loadThreadsFromServer() {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function (data) {
this.setState({ data: data });
}.bind(this),
error: function (xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
console.error(this.props.url, status, err.toString() 下划线。
由于看起来错误似乎与从服务器提取 JSON 数据有关,我尝试从空白数据库开始,但错误仍然存在。该错误似乎是在无限循环中调用的,大概是因为 React 不断尝试连接到服务器并最终导致浏览器崩溃。
编辑:
我使用 Chrome 开发工具和 Chrome REST 客户端检查了服务器响应,数据似乎是正确的 JSON。
编辑 2:
看起来虽然预期的 API 端点确实返回了正确的 JSON 数据和格式,但 React 正在轮询 http://localhost:3000/?_=1463499798727 而不是预期的 http://localhost:3001/api/threads。
我正在端口 3000 上运行 webpack 热重载服务器,并在端口 3001 上运行 express 应用程序以返回后端数据。令人沮丧的是,我上次处理它时它工作正常,但找不到我可能改变的东西来破坏它。
【问题讨论】:
-
这表明您的“JSON”实际上是 HTML。查看您从服务器返回的数据。
-
如果您执行
JSON.parse("<foo>")之类的操作会出现此错误——JSON 字符串(您期望使用dataType: 'json')不能以<开头。 -
正如@quantin 所说,它可以是 html,也可能是某种错误,请尝试与其他客户端相同的 url
-
就像我提到的,我用一个空数据库(它只返回 [])尝试它,它仍然给出相同的错误
-
您很可能需要代理 API 请求,具体取决于您的
NODE_ENV。看到这个:github.com/facebookincubator/create-react-app/blob/master/…
标签: javascript json reactjs