如何修复:意外的令牌&lt;在JSON位于JSON.parse的位置0(<anonymous>)

时间:2017-10-07 20:41:34

标签: json mongodb angular typescript mongoose

  

我不断收到这篇文章标题中的错误。所以我从response.json()中删除了我的.json()   我的下面代码return this.http.post('http://localhost:3000/sign-up', body, {headers: headers}) .map((response: Response) => response.json())   但是,现在我得到一个错误:

Response {_body: "<!DOCTYPE html>↵<html lang="en">↵<head>↵    <base …src="/js/app/bundle.js"></script>↵</body>↵</html>",

我是网络开发的新手,我尝试使用我在线课程中的代码。我知道我的console.log应该是一个对象,而不是这个&#39; Response&#39;。如何将返回的数据作为我通过html表单提交的信息?

另外,我将.json()留在下面发布的代码中,这样就显示了我最初得到的Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)错误。但就像我说的那样,我删除了.json()并得到了Response {_body: "<!DOCTYPE html>↵<html lang="en">↵<head>↵ <base …src="/js/app/bundle.js"></script>↵</body>↵</html>",错误

import { Injectable } from "@angular/core";
import { Http, Headers, Response } from "@angular/http";
import 'rxjs/Rx';
import { Observable } from "rxjs";

import { User } from "./user.model";
import { ErrorService } from "../errors/error.service";

@Injectable()
export class AuthService {
    constructor(private http: Http, private errorService: ErrorService) {}

    signup(user: User) {
        const body = JSON.stringify(user);
        const headers = new Headers({'Content-Type': 'application/json'});
        return this.http.post('http://localhost:3000/sign-up', body, {headers: headers})
            .map((response: Response) => response.json());
            // .catch((error: Response) => {
            //     this.errorService.handleError(error.json());
            //     return Observable.throw(error.json());
            // });
    }
}
  

下一课

import { Component, OnInit } from "@angular/core";
import { FormGroup, FormControl, Validators } from "@angular/forms";

import { AuthService } from "./auth.service";
import { User } from "./user.model";

@Component({
    selector: 'app-signup',
    templateUrl: './signup.component.html'
})
export class SignupComponent implements OnInit {
    myForm: FormGroup;

    constructor(private authService: AuthService) {}

    onSubmit() {
        const user = new User(
            this.myForm.value.email,
            this.myForm.value.password,
            this.myForm.value.firstName,
            this.myForm.value.lastName,
            this.myForm.value.age,
            this.myForm.value.goal
        );
        this.authService.signup(user)
            .subscribe(
                data => console.log(data),
                error => console.error(error)
            );
        this.myForm.reset();
    }

    ngOnInit() {
        this.myForm = new FormGroup({
            firstName: new FormControl(null, Validators.required),
            lastName: new FormControl(null, Validators.required),
            email: new FormControl(null, [
                Validators.required,
                Validators.pattern("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
            ]),
            password: new FormControl(null, Validators.required),
            age: new FormControl(null, Validators.required),
            goal: new FormControl(null, Validators.required)
        });
    }
}

1 个答案:

答案 0 :(得分:0)

问题在于您收到的内容,当您的代码期待HTML内容时,您收到的内容为json。您所要做的就是将服务器配置为返回json,或修改代码以处理HTML响应。