SyntaxError: Unexpected token < in JSON at position 0

Im getting this error on this function

this.http.request("")
.subscribe((res) => {
        console.log(res.json())
        
        this.list = res.json();
    }, (err) => {
        console.log(err)
    })

I left request first parameter a empty string, what should i give here, firebase address like this? https://xyz.firebaseio.com, im getting same error

The first parameter for this.http.request("") should be the URL you are trying to get data from.
As you are trying to convert the response you receive to JSON - you need to provide an URL which can return JSON response. (https://xyz.firebaseio.com doesn’t return a JSON response)

try out
this.httpService.get(“http://jsonplaceholder.typicode.com/posts”)
.subscribe((res) => {console.log(res.json())})

what is json placeholder, and the whole address i mean im unable to understand my data is at firebase how can i get data from another url like you said jsonplaceholder.typicode

@yoyodj As @sankar_gopalan mentioned, the first parameter should be a URL which returns JSON data. Let’s say that your Firebase URL is https://xyz.firebaseio.com, then you can’t use just https://xyz.firebaseio.com because it doesn’t return JSON - it returns HTML, which means that you have to specify that you want JSON data: https://xyz.firebaseio.com/.json. Now the returned data is in JSON format but it returns the whole content of the Firebase instance, which you probably don’t want. Let’s say that you want to retrieve all posts which are stored in posts under the Firebase root, then your URL should be https://xyz.firebaseio.com/posts.json. Also don’t forget to use a real Firebase URL, i.e. xyz should be the name of the Firebase instance that you’re trying to get data from, which means that you should replace it with the Firebase data URL of your Firebase account.

this.http.request('https://xyz.firebaseio.com/posts.json')
    // Call map on the response observable to get the parsed object.
    .map((res) => res.json())
    // Subscribe to the observable to get the parsed object and use it.
    .subscribe((posts) => {
	    console.log(posts);
	    this.list = posts;
    }, (err) => {
	    console.log(err);
    });

Suppose I want to get title:“fjslk” of this KF6dIZnUliIVtNykDEN(what is it called btw) then my url would be like this

this.http.request("https://taskionic.firebaseio.com/firebaseList.json")
.map((res) => res.json()).subscribe((res) => {console.log(res.json)}

as response i got Object {-KF6dIZnUliIVtNykDEN: Object} at the console, also adding this line .map((res) => res.json()) giving me error .map is not defined,
how can i do this

@yoyodj Sorry, I forgot to mention that you have to import it, add this to your imports:

import 'rxjs/add/operator/map';

The string KF6dIZnUliIVtNykDEN is a Firebase unique ID - it’s generated automatically when you add new data to a Firebase path using push().

Here is a working plunker demo, you just have to add your Firebase data URL in home.ts.

Oh yes can i build facebook like application in this like if i create post(it will be object in the list with unique id) and when somebody comments it should make sure belong to that post(i think with unique id its possible, can i retrieve id)

Thanks man! This post got me through my troubles, getting correctly formatted JSON data now :wink:

Hi @iignatov ,

Could you help me how to get the token ??
I am using backendless.com service.
My result after succesfull login was like this…

Object {lastLogin: 1463742055662, created: 1456890510000, name: "personal", ___class: "Users", user-token: "7652C01C-7343-F9AE-FF15-DD7E0427A800"…}
___class:"Users"
__meta:"{"relationRemovalIds":{},"selectedProperties":["__updated__meta","password","created","name","___class","ownerId","updated","objectId","email"],"relatedObjects":{}}"
      created:1456890510000
      email:"personal@gmail.com"
      lastLogin:1463742055662
      name:"personal"
      objectId:"C8FE36A2-9396-13C5-FF9C-8F8EF7F07900"
      ownerId:null
      updated:null
      user-token:"7652C01C-7343-F9AE-FF15-DD7E0427A800"
__proto__:Object

Please look at my console.log at my user_service.js.
I can console.log email or objectId but fail for user-token.
My problem is how to get the user-token and save it to localstorage so i can use it for another purpose??
my user_service.js

import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Headers, RequestOptions} from 'angular2/http';
import {Observable}     from 'rxjs/Observable';
import {BackendService} from './backend_service';
import {Storage, LocalStorage, Events} from 'ionic-angular';
import 'rxjs/Rx';

@Injectable()
export class UserService {

    static get parameters() {
        return [[Http], [BackendService], [Events]];
    }

    constructor(http, backendService, events) {
        this.http = http;
        this.backendService = backendService;
        this.events = events;
        this.user = {};
        this.storage = new Storage(LocalStorage);
        this.HAS_LOGGED_IN = 'hasLoggedIn';
        this.Backend_Login_Url = backendService.BackendLoginUrl
        this.Backend_Register_Url = backendService.BackendRegisterUrl
        this.Backend_Logout_Url = backendService.BackendLogoutUrl
        this.Backend_Header = backendService.BackendHeader
    }

   userLogin(userData)
        {
        let body = JSON.stringify({ login: userData.email, password: userData.password });
        let headers = new Headers(this.Backend_Header);
        let options = new RequestOptions({ headers: headers });
        return this.http.post(this.Backend_Login_Url, body, options)
            .map((res) => {
               let data = res.json();
               console.log('data.email at user_service', data.email); // result : personal@gmail.com
               console.log('data.objectId at user_service', data.objectId); // result C8FE36A2-9396-13C5-FF9C-8F8EF7F07900
               console.log('data.user-token at user_service', data.user-token); // result Reference error : Token is not defined
              this.storage.set('userStorage', JSON.stringify(data)); // result : success
              return data;
         });
}

Thks in advance.

You can’t use ‘-’ in variable name.
data[‘user-token’]

1 Like

@sciu You have to use data['user-token'], as @xr0master suggested.

It works…

Thks for @xr0master suggestion
and thks @iignatov for ur reply…