# Json call on unauthorized url returns Unexpected token \< in JSON at position

**URL:** <https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620>\
**Category:** General\
**Created:** [March 10, 2020, 6:59am UTC](https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620 "2020-03-10T06:59:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuva\_raj](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/yuva_raj/32/16963_2.png) [@yuva\_raj](https://discuss.emberjs.com/u/yuva_raj)\
**Post date:** [March 10, 2020, 6:59am UTC](https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620/1 "2020-03-10T06:59:22Z")

</div>

let json = this.\_super(url, type, options); Making ajax call on return it will return login page since url is unauthorized the json returned is

SyntaxError: Unexpected token \< in JSON at position 2

at JSON.parse ()

at jQuery.parseJSON (jquery.js:8520)

at ajaxConvert (jquery.js:8846)

at done (jquery.js:9264)

at XMLHttpRequest.callback (jquery.js:9718)

**Errors occured:** ember-metal.js:3992 TypeError: Cannot read property ‘get’ of undefined

at adapter.js:28

at tryCatcher (rsvp.js:215)

at invokeCallback (rsvp.js:393)

at publish (rsvp.js:379)

at publishRejection (rsvp.js:314)

at rsvp.js:14

ember-metal.js:3992 SyntaxError: Unexpected token \< in JSON at position 2

at JSON.parse ()

at jQuery.parseJSON (jquery.js:8520)

at ajaxConvert (jquery.js:8846)

at done (jquery.js:9264)

at XMLHttpRequest.callback (jquery.js:9718)

What may be the reason ?

---

<div class="post-metadata">

**Author:** ![Benjy1979](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@Benjy1979](https://discuss.emberjs.com/u/Benjy1979)\
**Post date:** [March 10, 2020, 8:51pm UTC](https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620/2 "2020-03-10T20:51:16Z")

</div>

Wouldn’t that be expected if the reply is not JSON? It’s trying to parse html as json

---

<div class="post-metadata">

**Author:** ![yuva\_raj](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/yuva_raj/32/16963_2.png) [@yuva\_raj](https://discuss.emberjs.com/u/yuva_raj)\
**Post date:** [March 11, 2020, 9:52am UTC](https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620/3 "2020-03-11T09:52:40Z")

</div>

Hiii Benjy Thanks for the reply. Then how to handle if html is returned or some error msg is returned

> <https://stackoverflow.com/questions/60616018/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position>

---

<div class="post-metadata">

**Author:** ![Benjy1979](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@Benjy1979](https://discuss.emberjs.com/u/Benjy1979)\
**Post date:** [March 11, 2020, 10:06am UTC](https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620/4 "2020-03-11T10:06:22Z")

</div>

These might help

[https://guides.emberjs.com/release/routing/loading-and-error-substates/](https://guides.emberjs.com/release/routing/loading-and-error-substates/)

---

<div class="post-metadata">

**Author:** ![yuva\_raj](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/yuva_raj/32/16963_2.png) [@yuva\_raj](https://discuss.emberjs.com/u/yuva_raj)\
**Post date:** [March 11, 2020, 10:47am UTC](https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620/5 "2020-03-11T10:47:37Z")

</div>

This call is made in adapter, i should handle failure of that ajax call and modify the url. is there is any way for that

---

<div class="post-metadata">

**Author:** ![Benjy1979](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@Benjy1979](https://discuss.emberjs.com/u/Benjy1979)\
**Post date:** [March 11, 2020, 10:51am UTC](https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620/6 "2020-03-11T10:51:51Z")

</div>

Is this an adapter you’ve already customised? And did you try to see if the error hook on the route is called?

I’m making assumptions a bit but I’m guessing the ajax promise will fail when ember tries to parse the json so would then call error on the route. Then from the route you’d get a chance to redirect to another route say, to log in.

If you do it from the adapter I’m supposing you are not going to get much chance to act on it. If the above error hook in the route does not get called you could just return your own ajax call and fail the promise deliberately.

---

<div class="post-metadata">

**Author:** ![yuva\_raj](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/yuva_raj/32/16963_2.png) [@yuva\_raj](https://discuss.emberjs.com/u/yuva_raj)\
**Post date:** [March 11, 2020, 11:00am UTC](https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620/7 "2020-03-11T11:00:43Z")

</div>

Yes I have customised the adapter, which should return json to route, If error hook in ember gets called can I get error code there.

The problem is I can’t handel the failure case…

let json = this.\_super(url, type, options);

since this variable ‘json’ contains this value ‘SyntaxError: Unexpected token \< in JSON at position 2’

I don’t know where json has been parsed…

---

<div class="post-metadata">

**Author:** ![Benjy1979](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@Benjy1979](https://discuss.emberjs.com/u/Benjy1979)\
**Post date:** [March 11, 2020, 11:06am UTC](https://discuss.emberjs.com/t/json-call-on-unauthorized-url-returns-unexpected-token-in-json-at-position/17620/8 "2020-03-11T11:06:07Z")

</div>

Is it related to this then [https://github.com/emberjs/data/issues/6936](https://github.com/emberjs/data/issues/6936)

You could just override the adapter call createRecord and create your own request I suppose, then you could catch the return value and return whatever structure you want.

```
 createRecord(store, type, snapshot) {
    let url = this.buildURL(type.modelName, null, snapshot, 'createRecord');
    let data = this.serialize(snapshot);
    data.action = "saveplan";
    return this.ajax(url, 'POST', { data });
},

```

Above is an example I have but I’m just thinking if the ajax call was replaced with a more customised one you would be able to just use the promises to detect it and translate the response to something that is more helpful for your application/route.
