am trying to make an ajax request following an old tutorial :
ajax: service(),
async authenticate(username, password) {
let response = await this.ajax.post('/token', {
headers: {
'Accept': 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json',
},
data: JSON.stringify({
username,
password })
})
let { user_email: userEmail, token } = response; return { userEmail, token };
}```
what am doing is :
```import { inject as service } from '@ember/service';
@service ajax;
async authenticate(email, password){
let response = await this.ajax.post('tokens', {
headers: {
'Accept': 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json'
},
data: JSON.stringify({
email,
password
})
})
let {user_email: userEmail, token} = response;
return {UserEmail, token};
}```
and what i get is that
Error: Assertion Failed: Attempting to inject an unknown injection: 'service:ajax'
This just means that you’re trying to inject a service called “ajax” but you don’t have one defined. You could either define one or, probably a better option, is to just use the fetch
API as demonstrated in the Ember tutorial. This is a more modern way to make AJAX requestts and doesn’t require defining a custom service.
1 Like
thanks, small question the ajax service not existed newer versions of ember why ? and did it exists before the es6 fetch showed up ?
I totally forgot about this but yes there is an addon called ember-ajax that used to be a default dependency, and I believe it provided that service. I believe it was removed somewhere in the Octane edition roadmap because of the desire to move away from jQuery as a (necessary) dependency of Ember. I’m not sure exactly when/what version that was removed but I’m pretty sure it was somewhere in the 3.x releases.
So if you’re using a modern version of Ember with jQuery disabled you wouldn’t want to use that addon as it would then require including jQuery. If you’re comfortable depending on jQuery in the long term you could probably enable jQuery and then install ember-ajax, but I’d still recommend using fetch
instead.
1 Like