“ SyntaxError:JSON中位置0处的意外标记<”

在处理类似于Facebook的内容提要的React应用程序组件中,我遇到了一个错误:

Feed.js:94未定义“ parsererror”“ SyntaxError:JSON中位置0处的意外令牌<

我遇到了类似的错误,事实证明这是render函数中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热重载服务器,而Express应用程序在端口3001上运行,以返回后端数据。令人沮丧的是,这是我上次对其进行处理时正确执行的操作,并且找不到我可能要更改的内容来破坏它。

回答:

错误消息的措词与运行时从GoogleChrome浏览器得到的内容相对应JSON.parse('<...')。我知道您说服务器正在设置Content-Type:application/json,但是我被认为是响应 主体 实际上是HTML。

Feed.js:94 undefined "parsererror" "SyntaxError: Unexpected token < in JSON

at position 0"

console.error(this.props.url, status, err.toString())下划线标出。

err内部实际上抛出jQuery,并传递给你作为一个变量err。带下划线的原因仅仅是因为这是您记录它的地方。

我建议您将其添加到日志记录中。查看实际的xhr(XMLHttpRequest)属性以了解有关响应的更多信息。尝试添加console.warn(xhr.responseText),您很可能会看到正在接收的HTML。

以上是 “ SyntaxError:JSON中位置0处的意外标记&lt;” 的全部内容, 来源链接: utcz.com/qa/418732.html

回到顶部