Ajax request setup

Looking at official docs, the use of
app.request.setup(parameters) or Framework7.request.setup(parameters) is not recommended.

Why? Any better way to automatically setup authorization when executing ajax request to an API service ?

I can set our custom headers every time, but I don’t think this would be different from using the setup method

1 Like

It is not recommended because many don’t really sure what it is and how to use it, so it can easily break the things :slight_smile: For auth headers it fits perfectly

1 Like

Ok, perfect.
How can I ‘reset’ the configuration? I’ve tried with app.request.setup({}); but does’t work. It should configure as an empty object, thus removing the old configuration…

This because i’m setting the authorization header during the login phase, but if login doesn’t succeed, I would like to remove the bad credential and start from scratch.

this is why it is not recommended

you have to re-extend the obj
with an empty value

app.request.setup({
  headers:{
    Authorization:''
  }
});

or override the setup in the request

app.request({
  headers:{
    Authorization:''
  },
  url:''
});

this is what I do now.
but is forcing me to re-extend the object with all values specified one by one, because a simple:

app.request.setup({
  headers:{}
});

or even better

app.request.setup({ });

is not working, because empty objects are ignored.

this is how extend works
btw, everything in F7 is extended

here is an example to show you how it works

open console
copy/paste this steps (one by one)

//step1
var obj1={};
console.log(obj1); //=> {}
//step2
var obj2={
  headers:{
    Authorization:'ok'
  }
};
app.utils.extend(obj1,obj2);
console.log(obj1); //=> {headers:{Authorization:'ok'}}
//step3
var obj3={
  headers:{
  }
};
app.utils.extend(obj1,obj3);
console.log(obj1) //=> {headers:{Authorization:'ok'}}
//step4
var obj4={
  headers:{
    Authorization:'ko'
  }
};
app.utils.extend(obj1,obj4);
console.log(obj1) //=> {headers:{Authorization:'ko'}}
1 Like