Facebook のようなコンテンツ フィードを処理する React アプリ コンポーネントで、エラーが発生しています。
Feed.js:94 未定義の "parsererror" "SyntaxError: 位置 0 の JSON に予期しないトークン < があります
同様のエラーに遭遇しましたが、レンダリング関数内の 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
。
私は、バックエンド データを返すためにポート 3001 で Express アプリを実行し、ポート 3000 で webpack ホット リロード サーバーを実行しています。ここでイライラするのは、前回作業したときには正常に動作していたのに、何を変更したら動作がおかしくなったのかがわからないことです。
ベストアンサー1
エラー メッセージの文言は、 を実行したときに Google Chrome から取得される内容と一致します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 が表示される可能性が高くなります。