【问题标题】:SyntaxError: Unexpected token < in JSON at position 3 in ionic 2SyntaxError: Unexpected token < in JSON at position 3 in ionic 2
【发布时间】:2017-01-17 22:24:23
【问题描述】:

Ionic 2 中出现错误“SyntaxError: Unexpected token

下面是我的spring boot代码。

感谢您的帮助。

@RequestMapping(value="/myview", method=RequestMethod.GET, produces = "application/json")
    @ResponseBody
    List<Client> myView( @ModelAttribute("client") Client client){


        List<Client> data=(List<Client>) clientService.getAll();


        return data;
    }

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class PeopleService {
  people: any;

  constructor(public http: Http) {}

load(){

    if (this.people) {

  return Promise.resolve(this.people);

  }

 return new Promise(resolve => {
    this.http.get('http://localhost:8080/myview')
    .map((res)=>res.json()).subscribe((data)=>{
       console.log(data);
       this.people=data;
       resolve(this.people);
     }, err=>{console.log(err)});
  });
}// end load function

}

来自 /myview 的 JSON

[{"id":1,"username":"donald@yahoo.com","policy":"V121293031","name":"Donald","mobile":"0504735260","email" :"dcgatan@gmail.com","address":"Dafza Dubai","amount":800.98,"datetimestamp":1472861297000},{"id":3,"username":"dcgatan78@gmail.com", "policyno":"V38998933","fname":"Donald","mobile":"0501234567","email":"dcgatan@gmail.com","address":"MetDubai","amount":334.34, "datetimestamp":1472862939000},{"id":4,"username":"dcgatan@yahoo.com","policyno":"V34342323","fname":"Snoopy","mobile":"0501234567", "email":"dcgatan@yahoo.com","address":"Metlife Dafza Dubai","amount":883.43,"datetimestamp":1472916463000}]

我的http://localhost:8080/myview 不工作,因为当我尝试使用数组值的以下代码时它工作。如何调用http而不是将静态值放入Array?

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class PeopleService {

  people: Array<any> = [{"id":1,"username":"donald@yahoo.com","policyno":"V121293031","fname":"Donald","mobile":"0504735250","email":"dcgatan@gmail.com","address":"Dafza Dubai","amount":800.98,"datetimestamp":1472861297000},{"id":3,"username":"dcgatan78@gmail.com","policyno":"V38998933","fname":"Donald","mobile":"0501234567","email":"dcgatan@gmail.com","address":"MetLife Dubai","amount":334.34,"datetimestamp":1472862939000}];


  constructor(private http: Http) {}

load(){

    if (this.people) {

  return Promise.resolve(this.people);

  }

 return new Promise(resolve => {

    this.http.get('http://localhost:8080/myview')
    .map((res)=>res.json())
    .subscribe((data)=>{
       this.setPeople(data);
       resolve(this.people);
     });
  });
}// end load function

setPeople(data) {
      if (data) {
        for (let id of Object.keys(data)) {
          let item = data[id];

          item.id = id;

          this.people.push(item);
        }
      }
    }

}

【问题讨论】:

    标签: json spring-mvc spring-boot ionic2


    【解决方案1】:

    您对 /myview 的调用将返回不正确的 json。它必须有 HTML 元素。执行 res.json() 从响应的 _body 中提取数据,如果它是有效的。但在您的情况下,它会引发错误。

    【讨论】:

    • 以上是我从 /myview 生成的 json
    • 做一件事。打开浏览器的开发人员控制台,在此处打开源代码并在此行应用调试器:(res)=>res.json())。当您运行代码时,执行将在那里暂停。此时,将鼠标悬停在“res”上,让我知道您看到了什么?如果您在开发者控制台中找不到您的代码,请编写调试器;之后 (res)=>res.json())
    • 请检查我是否正确 (res)=>res.json(): (res) arguments : [Exception: TypeError: 'caller' and 'arguments' are limited function properties and cannot be在这种情况下访问。在 Function.remoteFunction (:3:14)] caller : [Exception: TypeError: 'caller' 和 'arguments' 是受限制的函数属性,无法在此上下文中访问。在 Function.remoteFunction (:3:14)]
    • 为什么要访问调用堆栈?您是否暂停并将鼠标悬停在 res 上?
    • 另外,看看这个问题:stackoverflow.com/questions/36814694/…
    猜你喜欢
    • 2022-01-22
    • 2016-11-03
    • 2019-11-17
    • 2019-09-20
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    相关资源
    最近更新 更多