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

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 ?

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

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

These might help

https://guides.emberjs.com/release/routing/loading-and-error-substates/

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

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.

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…

Is it related to this then 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.