提问者:小点点

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


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

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数据有关,所以我尝试从一个空白db开始,但错误仍然存在。 错误似乎是在无限循环中调用的,可能是React不断尝试连接到服务器,最终导致浏览器崩溃。

编辑:

我已经用Chrome dev工具和Chrome REST客户端检查了服务器响应,数据看起来是正确的JSON。

编辑2:

看起来,虽然预期的APIendpoint确实返回了正确的JSON数据和格式,但React轮询的是http://localhost:3000/?_=1463499798727,而不是预期的http://localhost:3001/API/threads

我在端口3000上运行一个webpack热重新加载服务器,在端口3001上运行express应用程序以返回后端数据。 令人沮丧的是,上一次我在这上面工作的时候,它是正常工作的,但是我找不到我可以改变什么来破坏它。


共3个答案

匿名用户

错误消息的措辞与运行json.parse('<...')时从Google Chrome得到的内容相对应。 我知道您说服务器正在设置content-type:application/json,但我认为响应体实际上是HTML。

feed.js:94未定义的“ParserError”“syntaxError:意外的标记

其中console.error(this.props.url,status,err.ToString())行带下划线。

err实际上是在jQuery中抛出的,并作为变量err传递给您。 这一行加下划线的原因仅仅是因为这是您记录它的地方。

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

匿名用户

您正在从服务器接收返回的HTML(或XML),但数据类型:json告诉jQuery将其解析为JSON。 检查Chrome开发工具中的“网络”选项卡,查看服务器响应的内容。

匿名用户

这对我来说是一个权限问题。 我试图使用cancan访问一个我没有授权的url,因此该url被切换到users/sign_in。 重定向的url响应html,而不是JSON。 html响应中的第一个字符是<