猿问

“SyntaxError:意外的令牌<在位置0的JSON中”

“SyntaxError:意外的令牌<在位置0的JSON中”

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

Feed.js:94 undefined“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: ''
                  })
  },

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应用程序以返回后端数据。令人沮丧的是,这是我上一次工作时正常工作,无法找到我可以改变的东西来打破它。


子衿沉夜
浏览 1688回答 3
3回答

繁星淼淼

您正在从服务器接收HTML(或XML),但dataType: json它告诉jQuery要解析为JSON。查看Chrome开发工具中的“网络”标签,查看服务器响应的内容。

至尊宝的传说

这最终成为我的权限问题。我试图访问一个我没有使用cancan授权的网址,所以网址被切换到了users/sign_in。重定向的url响应html,而不是json。html响应中的第一个字符是<。
随时随地看视频慕课网APP
我要回答