“syntaxerror:意外标记< 在json中的位置0”

nfg76nw0  于 2021-10-10  发布在  Java
关注(0)|答案(21)|浏览(415)

在处理类似facebook内容提要的react应用程序组件中,我遇到了一个错误:
feed.js:94位置0处json中未定义的“parsererror”语法错误:意外标记<
我遇到了一个类似的错误,结果是在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 developer tools中,错误似乎来自此函数:

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

8yoxcaq7

8yoxcaq716#

我也面临同样的问题
我从$.ajax方法中删除了数据类型:“json”

7ivaypg9

7ivaypg917#

将响应定义为时会发生此错误 application/json 您将得到一个html作为响应。基本上,当您使用json响应为特定url编写服务器端脚本时会发生这种情况,但错误格式是html。

mmvthczy

mmvthczy18#

在我的例子中,我得到了这个正在运行的网页包,结果是在本地节点的某个地方出现了一些损坏。

rm -rf node_modules
npm install

…足以让它再次正常工作。

vawmfj5a

vawmfj5a19#

我遇到了这个错误“syntaxerror:expected token m in json at position”,其中token'm'可以是任何其他字符。
事实证明,在使用restconsole进行db测试时,我遗漏了json对象中的一个双引号,如{“name:”math“},正确的应该是{“name:”math“}
我费了很大的劲才弄明白这个笨拙的错误。恐怕其他人也会遇到类似的麻烦。

qzwqbdag

qzwqbdag20#

这最终成为我的权限问题。我试图访问一个我没有cancan授权的url,因此该url被切换到 users/sign_in . 重定向的url响应html,而不是json。html响应中的第一个字符是 < .

ct3nt3jp

ct3nt3jp21#

您正在从服务器接收html(或xml),但是 dataType: json 正在告诉jquery将其解析为json。检查chrome开发工具中的“网络”选项卡以查看服务器响应的内容。

相关问题