首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >“SyntaxError:位置0处的意外标记<”

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

Stack Overflow用户
提问于 2016-05-17 23:21:49
回答 30查看 1.2M关注 0票数 277

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

Feed.js:94未定义的"parsererror“"SyntaxError:位置0处的意外标记

我遇到了一个类似的错误,结果是呈现函数中的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 (
    
      
      
      
      
      
    
    )
  }
})

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 (
    
      Feed
      
        
      
    
    )
  }
})

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应用程序以返回后端数据。这里令人沮丧的是,这是正确的工作,我上次工作,但找不到我可以改变它,以打破它。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37280274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档