繁体   English   中英

错误语法错误:JSON中位置0上的意外令牌<

[英]ERROR SyntaxError: Unexpected token < in JSON at position 0

错误:

core.es5.js?0445:1084 ERROR SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>

情况:

我要达到的目的是预先选择用户先前已经做出的选择,并阻止他两次投票。


码:

component.html

<article class="panel panel-default">
    <div class="panel-body">
      {{ poll.title }}
      <br>
      <br>
      <form #form="ngForm">
        <fieldset [disabled]="alreadyVotedFor(-1)">
          {{ poll.counter1 }} votes <input type="radio" id="{{ poll.choice1 }}" name="my_radio" value="{{ poll.choice1 }}" (click)="onChoice1(form)" [checked]="alreadyVotedFor(1)">  {{ poll.choice1 }}
          <br>
          {{ poll.counter2 }} votes <input type="radio" id="{{ poll.choice2  }}" name="my_radio" value="{{ poll.choice2 }}" (click)="onChoice2(form)" [checked]="alreadyVotedFor(2)">  {{ poll.choice2 }}
        </fieldset>
      </form>

    </div>
    <footer class="panel-footer">
        <div class="author">
            {{ poll.username }}
        </div>
        <div class="config" *ngIf="belongsToUser()">
            <a (click)="onEdit()">Edit</a>
            <a (click)="onDelete()">Delete</a>
        </div>
    </footer>
</article>

component.ts

votes: any;

    ngOnInit() {
        this.pollService.voted(this.poll, localStorage.getItem('userId')).subscribe(
            data => {
                this.votes = data.votes;
                console.log("NGONINIT this.votes: "+ this.votes);
            },
            err => { console.log("NGONINIT ERROR: "+ err) },
            () => { console.log("SUBSCRIBE COMPLETE this.votes: "+ this.votes); }
        );
    }

    alreadyVotedFor(choice: number) {
      let result = "";
      if (this.votes) {
          console.log("THIS.VOTES: "+this.votes);
          for (var i = 0; i < this.votes.length; i ++) {
              if (this.votes[i].poll == this.poll.pollId) {
                  result = "disabled";
                  if (this.votes[i].choice == choice) {
                      result =  "selected";
                  }
              }
          }
      }
      return result;
  }

服务

voted(poll: Poll, userID: string) {
    return this.http.get('http://localhost:3000/'+userID)
                    .map(response => response.json());
}

路线/user.js

router.get('/:userid', function (req, res, next) {
  var userId = req.params.userid;
  User.findById(userID, function (err, user) {
    console.log("USER JSON? :"+user.json());
    return res.send(user.json());
  });
});

型号/用户

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');

var schema = new Schema({
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    password: {type: String, required: true},
    email: {type: String, required: true, unique: true},
    polls: [{type: Schema.Types.ObjectId, ref: 'Poll'}],
    votes: [{
      poll: {type: Schema.Types.ObjectId, ref: 'Poll'},
      choice: {type: Number},
    }],
});

schema.plugin(mongooseUniqueValidator);

module.exports = mongoose.model('User', schema);

当服务器使用HTML而不是JSON进行响应时,尤其是在未找到路由且响应为404页面时,将发生此错误。

API不太可能具有唯一安装在root上并且可以作为http://localhost:3000/'+userID使用的路由,这是因为在这种情况下服务器无法执行其他任何操作。

如果路线已安装到某个路径,例如

app.use('/user', userRoutes);

那么请求也应该执行到该路径

return this.http.get('http://localhost:3000/user/'+userID)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM