2016-09-09 3 views
0

Ionic 2 "SyntaxError : 위치 3의 JSON에서 예상치 못한 토큰 <"에 오류가 있습니다. " 내 json 형식은 스프링 부트를 사용하여 올바르게 구성됩니다.SyntaxError : 이온 2의 위치 3에서 JSON에서 예기치 않은 토큰 <

다음은 제 스프링 부트 코드입니다.

감사합니다.

@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 

} 

JSON/MYVIEW

[{ "ID": 1, "이름": "[email protected]", "정책": "V121293031", "이름": "도널드" , "mobile": "0504735260", "email": "[email protected]", "address": "Dafza Dubai", "amount": 800.98, "datetimestamp": 1472861297000}, { "id": 3, "username": "[email protected]", "policyno": "V38998933", "fname": "Donald", "mobile": "05", "email": "[email protected]" ":"MetDubai ","amount ": 334.34,"datetimestamp ": 1472862939000}, {"id ": 4,"username ":"[email protected] ","policyno ":"V34342323 ","fname " "Snoopy", "모바일": "05", "이메일": "[email protected]", "주소": "Metlife Dafza Dubai", "금액": 883.43, "datetimestamp": 1472916463000}]

http://localhost:8080/myview이 작동하지 않습니다. 배열 값으로 아래 코드를 시도했기 때문입니다. 배열에 정적 값을 넣는 대신 http를 호출하는 방법?

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":"[email protected]","policyno":"V121293031","fname":"Donald","mobile":"0504735250","email":"[email protected]","address":"Dafza Dubai","amount":800.98,"datetimestamp":1472861297000},{"id":3,"username":"[email protected]","policyno":"V38998933","fname":"Donald","mobile":"05","email":"[email protected]","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); 
     } 
     } 
    } 

} 

답변

0

/myview를 호출하면 잘못된 json이 반환됩니다. HTML 요소가 있어야합니다. res.json()을 수행하면 응답의 _body에서 데이터가 추출됩니다 (유효한 경우). 그러나 귀하의 경우 그것은 오류를 던지고 있습니다.

위의
+0

은 내 json이/myview에서 생성했습니다. –

+0

한 가지를하십시오. 브라우저의 개발자 콘솔을 열고 소스 코드를 열고 다음 줄에 디버거를 적용하십시오 : (res) => res.json()). 코드를 실행하면 실행이 일시 중지됩니다. 이 시점에서 'res'위로 마우스를 가져 가면 무엇이 보이는지 알려주시겠습니까? 개발자 콘솔에서 코드를 찾을 수 없다면 디버거를 작성하십시오. (res) => res.json()) – Bharat

+0

올바르게 입력했는지 확인하십시오. res.json() : (res) 인수 : [예외 : TypeError : '호출자'및 '인수 '은 제한된 함수 속성이므로이 컨텍스트에서 액세스 할 수 없습니다. [예외 : TypeError : '호출자'및 '인수'는 제한된 함수 속성이며이 컨텍스트에서는 액세스 할 수 없습니다. at Function.remoteFunction ( : 3 : 14)] –

관련 문제